Requête ne produisant pas les résultats souhaités

Voici la structure de la table de ma table – table_groupe

entrez la description de l'image ici

Le résultat dont j'ai besoin devrait être comme ci-dessous

Voici la requête que j'ai essayé d'utiliser. Mais ne produisant pas le résultat comme désiré.

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 

Cela pourrait être plus facile avec l'agrégation conditionnelle:

 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;