Akansha Akansha - 7 months ago 13
SQL Question

Querying a table for various combinations

I have a table

enter image description here

The above table is the master table. I want to devide them based on caption - like all plan,content and accuracy to group based on parentid.

enter image description here

Need help me with this. Thanks

Answer

You can use a PIVOT for this.

SELECT * FROM
    (SELECT ParentID, Caption, Name
        FROM #table) AS t
    PIVOT
    (
        MAX(t.Name)
        FOR Caption IN ([Plan], [Content], [Accuracy])
    ) AS pvt