我有一个需要排序的查询,然后我需要从中选择特定的行。无法在查询中订购
错误:
Additional information: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
我所试图做的是以下几点:
"SELECT * FROM (SELECT" +
" Websites.Id as websiteId, " +
" Websites.Title, " +
" Websites.Description, " +
" Websites.Url, " +
" Websites.BannerURL, " +
" (Select Count(*) From Votes where WebsiteID = Websites.Id And [Unique] = 1 And Date = '" +
Date + "') as TotalVotes, " +
" ISNULL((Select AVG(rating) From WebsiteRating where WebsiteID = Websites.Id), 5) as Rating, " +
" Users.Username, " +
" (Select Count(*) From Redirects where WebsiteID = Websites.Id And [Unique] = 1 And Date = '" +
Date + "') as Redirects, " +
" RowNum = ROW_NUMBER() OVER (ORDER BY Websites.ID) " +
" FROM Websites " +
" INNER JOIN Users ON Websites.UserID = Users.Id " +
" Where Websites.Enabled = 1" +
" GROUP BY Websites.Title, Websites.Description, Websites.Url, Websites.BannerURL , Users.Username, Websites.Id" +
// Error
" ORDER BY Websites.Id DESC" +
") as Table1 " +
"WHERE RowNum > " + number + " And RowNum <= " + amount + "";
当我通过后做的顺序:
"WHERE RowNum > " + number + " And RowNum <= " + amount + "";
然后首先选择从0到25的行,然后排序。但我想先订购它,然后从该列表中选择25行。
我仍然是一个初学sql,总是与Linq合作。但这是我的一个旧的项目,仍然可以使用普通的sql。
谢谢,如果我想按子查询命令:TotalVotes? – Jamie 2014-09-27 10:44:55
您可以在子查询完成后使用我介绍的where子句位置按任意列进行排序。但是,记住你的行选择是基于特定的顺序,所以如果你改变了演示顺序,你还需要改变RowNum的计算方式。 – 2014-09-27 12:50:38