【发布时间】:2020-07-15 14:14:07
【问题描述】:
这是我的包含数据透视的 SQL:
select * from (
select
[event_id]
,[attnum]
,PollId
,PollResponseDisplayText
from
[dbo].[v_PollReportDetails2]
) as tmp
pivot (max(tmp.[PollResponseDisplayText])
for tmp.PollId in ([703],[805],[806],[807],[808],[809])) as pivot_table
我想把数据透视表改成这样:
for tmp.PollId in (select PollId
from Polls
where event_id = 100100
and isVisible = 1)) as pivot_table
我可以在一个存储过程中完成这一切,并动态生成一条 SQL 语句以输入 execute() 语句,但我需要能够在视图中执行此操作。
【问题讨论】:
-
能否请您添加一些表格的示例数据
-
你不能 - 视图只能有静态定义(至少在 SQL 服务器中 - 你没有标记你的 DBMS)。
-
PIVOT 子句要求在查询设计时(查询执行之前)知道这些不同的值。
标签: sql sql-server tsql pivot