计算列上的DENSE_RANK

时间:2012-01-06 06:41:13

标签: sql sql-server sql-server-2008

CASE 
          WHEN [col1] = 's' THEN 8
          WHEN [col1] = 't' THEN 6 
          WHEN [col1] = 'u' THEN 9
          WHEN [col2] = 'v' THEN 1
.......
END AS product,
DENSE_RANK() OVER (ORDER BY product ASC) AS [Rank]
FROM dbo.TableA

DENSE_RANK()无法使用计算列Product,显示

  

列名'product'无效。

如何在产品上应用DENSE_RANK()

1 个答案:

答案 0 :(得分:3)

你可以这样做:

SELECT 
  *, 
  DENSE_RANK() over (order by product)
FROM (
SELECT 
  CASE 
          WHEN [col1] = 's' THEN 8
          WHEN [col1] = 't' THEN 6 
          WHEN [col1] = 'u' THEN 9
          WHEN [col2] = 'v' THEN 1
 ...
END AS product
FROM dbo.TableA
) t