繁体   English   中英

PostgreSQL查询将行合并为一

[英]postgresql query merge rows into one

select ds.school_name_np,
case
    when (ds.gender_id = 1 and ds.class=1) then ds.no_of_student 
end as boys_1,
case
    when (ds.gender_id = 2 and ds.class=1) then ds.no_of_student
end as girls_1,
case
    when (ds.gender_id = 1 and ds.class=2) then ds.no_of_student
end as boys_2,
case
    when (ds.gender_id = 2 and ds.class=2) then ds.no_of_student
end as girls_2
from data_047_differntly_abled_school_summary ds
-- GROUP BY school_name_np, gender_id, class, no_of_student
ORDER BY school_name_np

我有上面的查询,生成结果如下:

在此处输入图片说明

如何单行获得同一所学校的成绩?

只需在每列上使用聚合函数:

select ds.school_name_np,
       MAX(case when ds.gender_id = 1 and ds.class=1 then s.no_of_student end) as boys_1,
       MAX(case.....),
       ...
from data_047_differntly_abled_school_summary ds
GROUP BY school_name_np
ORDER BY school_name_np

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM