开发者

SQL Unpivot with row numbers

I have a SELECT statement with UNPIVOT

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

And returns values like

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

How can I add a row number before the row dycd_acnam?开发者_开发百科 Tried RANK() but it returned wrong values. I want to avoid using #Temp tables as much as possible.


If you just want an incrementing column added to the result you can add

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

to the SELECT list

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜