我试图按特定日期筛选结果,但当我使用下面的代码时,我只得到CloseDate与当前日期匹配的结果。当CloseDate为空时,它不包括ResolvedDate的结果。
我试过:
(CASE WHEN (Closedate IS Null) then ResolvedDate, ELSE CloseDate END) AS FinalDate
但它接着说:
"column FinalDate does not exist"
还有别的办法吗?
这是目前为止的代码。谢谢你的帮助。
SELECT
id,
(CASE WHEN (Closedate IS Null) then ResolvedDate, ELSE CloseDate END),
FROM cases
WHERE (EXTRACT (month FROM Closedate) = EXTRACT(month FROM current_date))
AND ( EXTRACT(day from Closedate) = EXTRACT(day FROM current_date))
最佳答案
您应该使用COALESCE函数http://www.postgresql.org/docs/current/static/functions-conditional.html
试试这个:
SELECT
id,
COALESCE(CloseDate, ResolvedDate) AS FinalDate
FROM
cases
WHERE
(EXTRACT (month FROM COALESCE(CloseDate, ResolvedDate)) = EXTRACT(month FROM current_date)) AND
(EXTRACT (day from COALESCE(CloseDate, ResolvedDate)) = EXTRACT(day FROM current_date))
应该可以。。。