Rows to columns based on date/time in Postgresql

我是研究僧i 提交于 2020-04-30 11:43:55

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!