如何使用数据透视函数,使用日期部分字段
How to use pivot function, using datepart fields
Current Output
WeekNumber WeeklyCount
11 100
10 200
9 300
8 400
7 500
Desired Output
WeekNumber 11 10 9 8 7
WeeklyCount 100 200 300 400 500
Current query:
SELECT Top 5 DatePart(ww, DATE) as weekNumber, Count(id) as WeeklyCount
FROM dbo.AL
Where DatePart(ww, DATE) <> DatePart(ww, GetDate())
group by DatePart(ww, DATE)
order by DatePart(ww, DATE) desc
How do I use the pivot function to return the desired output?
Pivot function query
试试这个:
SELECT [11], [10], [9], [8], [7]
FROM (YourQueryHERE) AS DT
PIVOT(SUM(WeeklyCount) FOR WeekNumber IN([11], [10], [9], [8], [7])) AS PT
如果你想要动态版,请看我的评论;)
Current Output
WeekNumber WeeklyCount
11 100
10 200
9 300
8 400
7 500
Desired Output
WeekNumber 11 10 9 8 7
WeeklyCount 100 200 300 400 500
Current query:
SELECT Top 5 DatePart(ww, DATE) as weekNumber, Count(id) as WeeklyCount
FROM dbo.AL
Where DatePart(ww, DATE) <> DatePart(ww, GetDate())
group by DatePart(ww, DATE)
order by DatePart(ww, DATE) desc
How do I use the pivot function to return the desired output?
Pivot function query
试试这个:
SELECT [11], [10], [9], [8], [7]
FROM (YourQueryHERE) AS DT
PIVOT(SUM(WeeklyCount) FOR WeekNumber IN([11], [10], [9], [8], [7])) AS PT
如果你想要动态版,请看我的评论;)