Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL PIVOT to generate required output

Tags:

sql

sql-server

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.

like image 225
w0051977 Avatar asked Dec 29 '25 07:12

w0051977


1 Answers

This should work:

SELECT ReviewType, DER, LEI, NOR, [NOT], LIN
FROM Src
PIVOT (SUM(Total) FOR OwningAgency IN (DER, LEI, NOR, [NOT], LIN)) P
like image 177
Paweł Dyl Avatar answered Dec 30 '25 22:12

Paweł Dyl