geocoder
geocoder

Reputation: 95

transpose a matrix in postgresql without using crosstab

In order to share some insights automatically in a slack channel, I need to created a transposed matrix from the input data i have, below you can find the input and output (used excel).

Please note that i cannot use tablefunc extention.

enter image description here

Upvotes: 0

Views: 154

Answers (1)

user330315
user330315

Reputation:

You can use filtered aggregation as shown in this answer

select cat, 
       max(lr_perc) filter (where day = 1) as "1", 
       max(lr_perc) filter (where day = 2) as "2", 
       max(lr_perc) filter (where day = 3) as "3", 
       .... 
from the_table
group by cat;

Upvotes: 1

Related Questions