SQL 2005 透视查询
我正在尝试掌握数据透视查询语法。
我有大约 20 个字段的聚合数据,全部与 1 个字段相关联,这是这 20 个字段的类别(我所说的字段是指列)。
它看起来像这样
Category1 column1 column2 column3
Category2 column1 column2 column3
等等...
我想做的是将这些结果转变成这样
Category1 Category2 Category3
column1 column1 column1
column2 column2 column2
column3 column3 column3
我的查询看起来像这样
SELECT TOP(3) category FROM
(
SELECT category FROM table
)p
PIVOT
(
AVG(column1) as column1, AVG(column2) as column2, AVG(column3) as column3)
FOR category IN category
) AS pvt;
我正在运行 SQL Server 2005。感谢您的任何帮助或建议!
I am trying to get a grip on the pivot query syntax.
I have about 20 fields worth of aggregate data all associated with 1 field, which is the category for those 20 fields (by fields I mean columns).
It looks like this
Category1 column1 column2 column3
Category2 column1 column2 column3
and so on...
What I am trying to do is pivot those results to come out like this
Category1 Category2 Category3
column1 column1 column1
column2 column2 column2
column3 column3 column3
My query looks like this
SELECT TOP(3) category FROM
(
SELECT category FROM table
)p
PIVOT
(
AVG(column1) as column1, AVG(column2) as column2, AVG(column3) as column3)
FOR category IN category
) AS pvt;
I am running SQL Server 2005. Thanks for any help or suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您确实想要 UNPIVOT。
I think you really want UNPIVOT.
我最终在 SSRS 中使用了矩阵,因为我永远无法弄清楚 PIVOT 查询的语法。
I ended up using a matrix in SSRS because I could never figure out the syntax of the PIVOT query.