【问题标题】:How would I duplicate the Rank function in a Sql Server Compact Edition SELECT statement?如何在 Sql Server Compact Edition SELECT 语句中复制 Rank 函数?
【发布时间】:2010-06-11 21:31:03
【问题描述】:

SQL Server Compact Edition 似乎不支持 RANK() 函数。 (请参阅函数(SQL Server Compact Edition)http://msdn.microsoft.com/en-us/library/ms174077(SQL.90).aspx)。

如何在 SQL Server Compact Edition SELECT 语句中复制 RANK() 函数。

(对于任何示例选择语句,请使用 Northwind.sdf,因为它是我可以使用 SQL Server 2005 Management Studio 打开的唯一一个。)

【问题讨论】:

    标签: sql tsql sql-server-ce analytic-functions


    【解决方案1】:
    SELECT x.[Product Name], x.[Unit Price], COUNT(y.[Unit Price]) Rank 
    FROM Products x, Products y 
    WHERE x.[Unit Price] < y.[Unit Price] or (x.[Unit Price]=y.[Unit Price] and x.[Product Name] = y.[Product Name]) 
    GROUP BY x.[Product Name], x.[Unit Price] 
    ORDER BY x.[Unit Price] DESC, x.[Product Name] DESC;
    

    解决方案修改自学生的排名 -Sql Compact Finding rank of the student -Sql Compact

    【讨论】:

    • +1:干得好,很好找。我更新了答案以包含 ANSI-92 JOIN 版本;您发布的是 ANSI-89 连接语法。
    【解决方案2】:

    用途:

      SELECT x.[Product Name], x.[Unit Price], COUNT(y.[Unit Price]) AS Rank 
        FROM Products x
        JOIN Products y ON x.[Unit Price] < y.[Unit Price] 
                      OR (    x.[Unit Price]=y.[Unit Price] 
                          AND x.[Product Name] = y.[Product Name]) 
    GROUP BY x.[Product Name], x.[Unit Price] 
    ORDER BY x.[Unit Price] DESC, x.[Product Name] DESC;
    

    以前:

    SELECT y.id,
           (SELECT COUNT(*)
             FROM TABLE x
            WHERE x.id <= y.id) AS rank
      FROM TABLE y
    

    【讨论】:

    • 不工作。 "解析查询时出错。[Token line number = 2,Token line offset = 9,Token in error = SELECT]"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多