开发者

SQL Unpivot with row numbers

开发者 https://www.devze.com 2023-04-10 17:19 出处:网络
I have a SELECT statement with UNPIVOT SELECT dycd_acnam, dycd_dynam, dycd_value FROM GCATT.dbo.tblDaycode UNPIVOT (

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

精彩评论

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