我遇到SQL语句问题。首先,这是我的DB2表中的值:
CLT_ID FP_NDT PRD_TCD CLP_DATE_CDTTM FP_SDT CLP_TCD CLP_DATE
------------------------------------------------------------------------------------------------
100085059 2015-06-30 01 2015-11-27-20.14.28.295209 2014-07-01 01 0001-01-01
100085059 2014-06-30 01 2015-11-27-20.14.28.284432 2013-07-01 01 0001-01-01
100085059 2015-06-30 01 2015-11-02-20.04.39.755865 2014-07-01 01 2026-01-30
100085059 2014-06-30 01 2015-10-15-00.00.00.001257 2013-07-01 01 2025-02-20
我的选择应该返回MIN行CLP_DATE值(0001-01-01)和最近的CLP_DATE_CDTTM。这是我的选择似乎不起作用:
SELECT MIN(CLP_DATE)
FROM TCDECF A
WHERE A.CLT_ID = 100085059
AND A.CLP_DATE >= '2015-10-15'
AND A.CLP_DATE_CDTTM =
(SELECT MAX(B.CLP_DATE_CDTTM)
FROM TCDECF B
WHERE B.CLT_ID = A.CLT_ID
AND B.FP_NDT = A.FP_NDT
AND B.PRD_TCD = A.PRD_TCD)
WITH UR;
此代码不返回任何内容。我想象它是因为" AND A.CLP_DATE> =' 2015-10-15"线。有没有办法说"选择任何> =比这个日期,但如果它什么都不返回,那么返回0001-01-01"?
答案 0 :(得分:1)
也许这样可行...从WHERE
子句中删除日期检查,而不是放在MIN
列表中的条件SELECT
中。使用COALESCE
返回该值,如果未找到则0001-01-01
(NULL
来自MIN
)。
SELECT COALESCE(MIN(case when A.CLP_DATE >= '2015-10-15' then A.CLP_DATE end),date'0001-01-01')
FROM TCDECF A
WHERE A.CLT_ID = 100085059
AND A.CLP_DATE_CDTTM =
(SELECT MAX(B.CLP_DATE_CDTTM)
FROM TCDECF B
WHERE B.CLT_ID = A.CLT_ID
AND B.FP_NDT = A.FP_NDT
AND B.PRD_TCD = A.PRD_TCD)
WITH UR;