views:

66

answers:

2

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  A: 

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
OMG Ponies
Not working. "There was an error parsing the query. [ Token line number = 2,Token line offset = 9,Token in error = SELECT ]"
AMissico
+1  A: 
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 http://stackoverflow.com/questions/2887096/finding-rank-of-the-student-sql-compact

AMissico
+1: Well done, good find. I updated my answer to include an ANSI-92 JOIN version; what you posted is ANSI-89 join syntax.
OMG Ponies