2010-06-11 19 views
1

它看起來不像SQL Server Compact Edition支持RANK()函數。 (請參閱函數(SQL Server Compact Edition) at http://msdn.microsoft.com/en-us/library/ms174077(SQL.90).aspx)。如何在Sql Server精簡版SELECT語句中複製Rank函數?

我該如何複製SQL Server Compact Edition SELECT語句中的RANK()函數。

(請使用Northwind.sdf任何樣本select語句,因爲它是唯一一個我可以與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; 

解決方案從學生的查找等級修改 - SQL的COM條約Finding rank of the student -Sql Compact

+0

+1:做得好,很好找。我更新了我的答案以包含ANSI-92 JOIN版本;你發佈的是ANSI-89連接語法。 – 2010-06-11 23:21:39

1

用途:

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 
+0

不工作。 「解析查詢時出現錯誤[令牌行號= 2,令牌行偏移量= 9,令牌出錯=選擇]」 – AMissico 2010-06-11 21:49:18