问题
Here is the part of the table:
I need to transform it to this:
I used aggregation but it fills with a lot of null cells. Of course there are more records with different timestamp in the table like : 09/02/2020 21:30:00 and so on.. I see similar question but I thing is somehow messed up in the sqlfiddle.
回答1:
You can achieve this using conditional aggregation.
select recordtime,
max(formattedvalue) filter (where fullname = 'FirstName') as "First Name",
max(formattedvalue) filter (where fullname = 'SecondName') as "Second Name",
max(formattedvalue) filter (where fullname = 'ThirdName') as "Third Name",
.... repeat for all possible full names ...
max(qualitydesc) as "QualityDesc",
max(statedesc) as "StateDesc"
from the_table
group by recordtime
order by recordtime;
来源:https://stackoverflow.com/questions/60188541/rows-to-columns-based-on-date-time-in-postgresql