簡體   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