如何使用pivot函数,使用datepart字段 [英] How to use pivot function, using datepart fields

查看:134
本文介绍了如何使用pivot函数,使用datepart字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当前输出

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

如果你想要动态版本,请看我的评论;)

If you want dynamic version, please see my comment ;)

这篇关于如何使用pivot函数,使用datepart字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆