下面是我的表-group_table的表结构
我需要的结果应该如下
下面是我尝试使用的查询。但没有产生预期的结果。
SELECT * FROM
(
SELECT A.PARENTID,A.NAME,A.CAPTION,A.PMKID,A.MKTPLANID,A.CATEGORY
FROM GROUP_TABLE A
)P
PIVOT
(
MAX(NAME)
FOR CAPTION IN ([Rollup],[Appeal Category],[PME])
)S
最佳答案
使用条件聚合可能会更容易:
select mktplanid,
max(case when caption = 'Rollup' then name end) as rollup,
max(case when caption = 'Appeal Category' then name end) as appealcategory,
max(case when caption = 'Planned Appeal' then name end) as plannedappeal,
max(case when caption = 'PME' then name end) as PME,
max(pmkid) as pmkid
from (select gt.*,
row_number() over (partition by caption, mktplanid ORDER BY mktplanid) as seqnum
from group_table gt
) gt
group by mktplanid, seqnum;
关于sql - 查询未产生期望的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852065/