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

家住魔仙堡 提交于 2019-12-24 10:49:10

问题


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/ms174077(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.)


回答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;

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




回答2:


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


来源:https://stackoverflow.com/questions/3026372/how-would-i-duplicate-the-rank-function-in-a-sql-server-compact-edition-select-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!