Skip to content
Advertisement

Pivot data row to header using SQL in Postgresql

I get this result using SQL request:

But i want result like this :

Can anyone help me to achieve this using PosgreSQL and thanks.

Advertisement

Answer

If you have a predefined list of values, you can use conditional aggregation to pivot:

This assumes that each nbr_type appears in just one row; otherwise, you possibly want to change the max() to a more meaningful aggregation function, such as sum() or avg().

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement