PostgreSQl:选择多行并将其排成一行

时间:2015-01-28 09:41:52

标签: sql postgresql pivot

我正在使用PostgreSQl,这是我在数据库中的表格:

enter image description here

如何编写一个给我这个结果的查询:

enter image description here

1 个答案:

答案 0 :(得分:0)

或者您可以编写像

这样的查询
select id_salarie, 
max(case when date_pointage = '2015-01-01' then round(nb_heures::numeric,2) else null end) as "2015-01-01",
max(case when date_pointage = '2015-01-02' then round(nb_heures::numeric,2) else null end)as "2015-01-02", 
max(case when date_pointage = '2015-01-03' then round(nb_heures::numeric,2) else null end) as "2015-01-03"
from my_table where id_salarie = 1
group by id_salarie;

查询看起来巨大而可怕但适用于多维数据集