SQL PIVOT 从列表中选择(IN SELECT)
是否可以执行数据透视并从表中选择列表,而不是使用单个值?
像这样(不正确的语法错误):
SELECT *
FROM (
SELECT RepID, MilestoneID, ResultID FROM RM
) AS src
PIVOT (
MAX(ResultID) FOR MilestoneID IN (SELECT id FROM m)
) AS pvt
这个可以编译,但对我不起作用:
SELECT *
FROM (
SELECT RepID, MilestoneID, ResultID FROM RM
) AS src
PIVOT (
MAX(ResultID) FOR MilestoneID IN ([1], [2], [3], [4])
) AS pvt
PS:我不想使用动态 SQL,有没有一种方法可以在不使用动态 SQL 的情况下做到这一点?
Is it possible to do a PIVOT and select list from a table, instead of using single values?
Like this (incorrect syntax error):
SELECT *
FROM (
SELECT RepID, MilestoneID, ResultID FROM RM
) AS src
PIVOT (
MAX(ResultID) FOR MilestoneID IN (SELECT id FROM m)
) AS pvt
This one compiles, but doesn't work for me:
SELECT *
FROM (
SELECT RepID, MilestoneID, ResultID FROM RM
) AS src
PIVOT (
MAX(ResultID) FOR MilestoneID IN ([1], [2], [3], [4])
) AS pvt
PS: I do NOT want to use dynamic SQL, is there a way to do this without using dynamic SQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果动态SQL出来了,恐怕答案是否定的,做不到。解析器需要预先知道这些值才能执行列转换。
If dynamic SQL is out then I'm afraid the answer is no, it can't be done. The parser needs to know the values up front to perform the pivot to columns.
这是可以做到的。
It Can be done.