我有一个类似这样的表,Report_table:
Report_DEFINITION_NAME | Report_CORE_NAME | COMPLETION_STATUS | COMPLOTION_DATE|
---|---|---|---|
报告AD | |||
报告BB | 错误 | 2022年6月24日||
报告AD | AD | 错误2020年3月19日 | |
报告R5 | |||
报告G8 | G8 | 错误04-06-2022 | |
报告R5 | |||
报告LH | LH | 成功2019年9月7日 | |
报告U6 | U6 | 错误 | 2022年5月12日 |
报告AD | AD | 成功2021年9月23日 |
所以,我使用了IN子句来解决我的问题,而没有Case表达式。
在select语句中获取最大值并使用父select语句中的in子句调用该语句解决了我的问题。类似于:
SELECT
a.*,
a.completion_date,
b.completion_date AS last_success_date
FROM report_table a, report_table b
WHERE a.report_definition_name = b.report_definition_name
AND a.completion_date IN
(
SELECT MAX(su.completion_date)
FROM report_table su
WHERE su.completion_status = 'Success'
AND b.report_definition_name = su.report_definition_name
)
ORDER BY a.completion_date DESC;