需要更正 SQL 个查询
Need correction of SQL query
我只想找到 mindate 所在的 semecode
我的查询是
SELECT stdcode, Degree_Code, TODATE, Gradediv, Degree_Name, semcode, mindate
FROM CHKLIST
WHERE stdcode LIKE '%02-11036%'
GROUP BY STDCODE, Degree_Code, TODATE, Gradediv, Degree_Name, semcode, mindate;
像输出这样的预期结果应该像附上这张图片:
此处附有示例数据
https://docs.google.com/spreadsheets/d/1NrU1NzVucmMLuwLXIf_nwvUbi6vjvEDkZDovsu5panA/edit?usp=sharing
如果你有一些 table 至少每个学期的开始日期,那会更容易,但如果你没有,这就是我所拥有的
select t1.*, cl.semcode
from (SELECT STDCODE, Degree_Code, TODATE, Gradediv, Degree_Name, min(mindate) mindate
FROM CHKLIST
GROUP BY STDCODE, Degree_Code, TODATE, Gradediv, Degree_Name) t1
join CHKLIST cl
on cl.stdcode = t1.stdcode
and cl.degree_code = t1.degree_code
and t1.gradediv = cl.gradediv
and t1.degree_name = cl.degree_name
and t1.mindate = cl.mindate
最复杂的方面是你有 semcode col 和难以比较的文本值。
数据库无法判断字符串“BAD-SUMMER-02”是否大于“BAD-FALL-01”。因此,我将 semcode 列执行到连接 table 中,然后再次使用相同的 table 连接来自子查询的聚合值。
我只想找到 mindate 所在的 semecode
我的查询是
SELECT stdcode, Degree_Code, TODATE, Gradediv, Degree_Name, semcode, mindate
FROM CHKLIST
WHERE stdcode LIKE '%02-11036%'
GROUP BY STDCODE, Degree_Code, TODATE, Gradediv, Degree_Name, semcode, mindate;
像输出这样的预期结果应该像附上这张图片:
此处附有示例数据
https://docs.google.com/spreadsheets/d/1NrU1NzVucmMLuwLXIf_nwvUbi6vjvEDkZDovsu5panA/edit?usp=sharing
如果你有一些 table 至少每个学期的开始日期,那会更容易,但如果你没有,这就是我所拥有的
select t1.*, cl.semcode
from (SELECT STDCODE, Degree_Code, TODATE, Gradediv, Degree_Name, min(mindate) mindate
FROM CHKLIST
GROUP BY STDCODE, Degree_Code, TODATE, Gradediv, Degree_Name) t1
join CHKLIST cl
on cl.stdcode = t1.stdcode
and cl.degree_code = t1.degree_code
and t1.gradediv = cl.gradediv
and t1.degree_name = cl.degree_name
and t1.mindate = cl.mindate
最复杂的方面是你有 semcode col 和难以比较的文本值。
数据库无法判断字符串“BAD-SUMMER-02”是否大于“BAD-FALL-01”。因此,我将 semcode 列执行到连接 table 中,然后再次使用相同的 table 连接来自子查询的聚合值。