SQL Unpivot与行号

时间:2011-10-04 06:14:10

标签: sql sql-server-2005 tsql

我有一个带有UNPIVOT

的SELECT语句
SELECT dycd_acnam, dycd_dynam, dycd_value
FROM GCATT.dbo.tblDaycode UNPIVOT (
dycd_value FOR dycd_dynam IN (
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
) AS pvtDaycode
GROUP BY dycd_acnam, dycd_dynam, dycd_value

并返回类似

的值
dycd_acnam  |  dycd_dynam  |  dycd_value
----------------------------------------
MT          | Monday       | 1
MT          | Tuesday      | 1
MT          | Wednesday    | 0
MT          | Thursday     | 0

如何在行dycd_acnam之前添加行号?尝试了RANK(),但它返回了错误的值。我想尽可能避免使用#Temp表。

1 个答案:

答案 0 :(得分:3)

如果您只想在结果中添加增量列,可以添加

ROW_NUMBER() OVER (ORDER BY (SELECT 0))

SELECT列表