开发者

How would I duplicate the Rank function in a Sql Server Compact Edition SELECT statement?

It doesn't look like SQL Server Compact Edition supports the RANK() function. (See Functions (SQL Server Compact Edition) at http://msdn.microsoft.com/en-us/library/ms174开发者_开发技巧077(SQL.90).aspx).

How would I duplicate the RANK() function in a SQL Server Compact Edition SELECT statement.

(Please use Northwind.sdf for any sample select statements, as it is the only one I can open with SQL Server 2005 Management Studio.)


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;

Solution modified from Finding rank of the student -Sql Compact at Finding rank of the student -Sql Compact


Use:

  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;

Previously:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜