我在postgresql上使用了一个返回这样结果的查询。
schedule | day | subject | grade |
===============================================
06 - 08 | Monday | Biology | Second |
-----------------------------------------------
08 - 10 | Monday | Chemistry | Third |
-----------------------------------------------
06 - 08 | Tuesday | Math | Second |
-----------------------------------------------
10 - 12 | Wednesday | English | Second |
-----------------------------------------------
10 - 12 | Friday | Language | Second |
-----------------------------------------------
我需要一个结果或像这样重新组织这个查询。
schedule | Monday | Tuesday| Wednesday | Friday | grade
==============================================================
06 - 08 | Biology | Math | null | null | second
--------------------------------------------------------------
10 - 12 | null | null | English |Language | second
--------------------------------------------------------------
我感谢任何帮助或建议。
Thnks。
答案 0 :(得分:0)
一种方法是条件聚合:
select schedule,
max(case when day = 'Monday' then subject end) as Monday,
max(case when day = 'Tuesday' then subject end) as Tuesday,
max(case when day = 'Wednesday' then subject end) as Wednesday,
max(case when day = 'Friday' then subject end) as Friday,
min(grade) as grade,
from t
group by schedule;
我不确定grade
的来源;以上只是猜测。