gpt4 book ai didi

sql - 重组 Postgrest 查询

转载 作者:行者123 更新时间:2023-11-29 13:52:23 25 4
gpt4 key购买 nike

我在 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
--------------------------------------------------------------

我感谢任何帮助或建议。

谢谢。

最佳答案

一种方法是条件聚合:

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;

我不太确定等级是从哪里来的;以上只是猜测。

关于sql - 重组 Postgrest 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38537487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com