w0051977 w0051977 - 2 months ago 11
SQL Question

SQL PIVOT to generate required output

Please see the data below:

enter image description here

I am looking for a query that generates the following output:

enter image description here

I am experimenting with 'PIVOT', but have not yet achieved the desired outcome.

Answer

This should work:

SELECT ReviewType, DER, LEI, NOR, [NOT], LIN
FROM Src
PIVOT (SUM(Total) FOR OwningAgency IN (DER, LEI, NOR, [NOT], LIN)) P
Comments