【发布时间】:2014-09-27 08:16:31
【问题描述】:
我有一个需要排序的查询,然后我需要从中选择特定的行。
错误:
附加信息:ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效,除非还指定了 TOP、OFFSET 或 FOR XML。
我想要做的是:
"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 行中的第 0 行。
我还是 sql 的初学者,一直使用 Linq。但这是我的一个旧项目,仍然可以使用普通 sql。
【问题讨论】: