简单的SQLServer PIVOT/Transpose查询,怎么写?
我有一个 SELECT,它以以下形式返回数据...
ID Question Answer
1 Any Good? Yes
1 Happy? No
1 Good Staff? Yes
1 Return? N/A
2 Any Good? No
2 Happy? No
2 Good Staff? Yes
2 Return N/A
...
我需要以下形式...
ID Any Good? Happy? Good Staff? Return?
1 Yes No Yes N/A
2 No No Yes N/A
...
我的顶级查询中的“Answer”列是使用 CASE .. WHEN
计算的。我想也许 PIVOT
子句可以帮助我。但这似乎需要聚合。我不需要聚合,只需转置即可。
我不介意必须在解决方案中显式指定每个 Answer
(我猜无论如何我都必须这样做)。
有人知道这样做的最佳方法吗?
I have a SELECT which is returning me data in the following form...
ID Question Answer
1 Any Good? Yes
1 Happy? No
1 Good Staff? Yes
1 Return? N/A
2 Any Good? No
2 Happy? No
2 Good Staff? Yes
2 Return N/A
...
I need this in the following form...
ID Any Good? Happy? Good Staff? Return?
1 Yes No Yes N/A
2 No No Yes N/A
...
The 'Answer' column in my top query is computed using a CASE .. WHEN
. I was thinking maybe the PIVOT
clause could help me. This seems to require aggregation though. I don't need to aggregate, just transpose.
I don't mind having to specify each Answer
explicitly in the solution (I'm guessing I'll have to do that anyway).
Anyone know the best way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有没有尝试过类似的东西
Have you tried something like
枢轴操作要求您使用某种形式的聚合,但是如果您只有一个值,
Max()
将为您获取最大(唯一)值,这样的东西应该可以工作
The pivot operation requires you to use some form of aggregate, however if you will only have one value
Max()
will grab the max (only) value for youSomething Like this should work