I get this result using SQL request:
x
NBR_TYPE Count
-----------------------
0021 20
0022 19
0023 13
But i want result like this :
0021 0022 0023
---------------------------------
20 19 13
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:
select
max(count) filter(where nbr_type = '0021') "0021",
max(count) filter(where nbr_type = '0022') "0022",
max(count) filter(where nbr_type = '0023') "0023"
from mytable
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()
.