【问题标题】:Can't order in query无法在查询中订购
【发布时间】: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。

【问题讨论】:

    标签: sql-server sql-order-by


    【解决方案1】:

    试试这个:

    "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" +
        ") as Table1 " +
        " WHERE RowNum > " + number + " And RowNum <= " + amount + "" +
        " ORDER BY RowNum DESC"
        ;
    

    您已经通过 RowNum 准备好有序数据,因此您不需要在子查询中使用 TOP 或 ORDER,只需将顺序应用于在最终 where 子句之后出现的被选中的行。

    【讨论】:

    • 谢谢,如果我想通过子查询订购:TotalVotes?
    • 您可以在子查询完成后使用我介绍的 where 子句位置按任何列排序。但是,请记住您的行选择是基于特定顺序的,因此如果您更改显示顺序,您还需要更改 RowNum 的计算方式。
    【解决方案2】:

    注释掉 ORDER BY Websites.Id DESC。 & 使用 order by desc this in

     RowNum = ROW_NUMBER() OVER (ORDER BY Websites.ID Desc) 
    

    或 在子查询中使用它。(上面没有评论)

    SELECT top 100 percent 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-14
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多