我试图按特定日期过滤结果,但是当我使用下面的代码时,我只得到CloseDate与当前日期匹配的结果。当CloseDate为Null时,它不包括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))
答案 0 :(得分:1)
假设你想要匹配年份:
SELECT id, COALESCE(Closedate, ResolvedDate) AS cdate
FROM cases
WHERE date_trunc('day', COALESCE(Closedate, ResolvedDate))
= date_trunc('day', now())
每个文档:COALESCE
,date_trunc()
如果你想忽略这一年:
WHERE to_char(COALESCE(Closedate, ResolvedDate), 'MMDD')
= to_char(now(), 'MMDD')
答案 1 :(得分:0)
您应该使用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))
应该这样做......