我正在尝试通过以下查询获取过去30天的计数-
SELECT date_occured, COUNT(*) FROM problem WHERE date_occured >= (CURRENT_DATE - 30) GROUP BY date_occured; //date_occured field is of type DATE.
基本上,在我的查询中,我尝试仅比较condition中的日期部分date_occured >= (CURRENT_DATE - 30),但似乎也比较时间。
date_occured >= (CURRENT_DATE - 30)
我按以下方式尝试了TRUNC-
TRUNC(date_occured) >= TRUNC(CURRENT_DATE - 30)
但是,当运行查询时,它永远不会返回。
我也试过-
SELECT date_occured, COUNT(*) FROM problem GROUP BY date_occured HAVING TRUNC(date_occured) >= TRUNC(CURRENT_DATE - 30);
再次,它永远不会返回。
如何在Oracle中仅比较两个DATE值中的日期部分?
对于这种情况,您只需要向右移TRUNC:
WHERE date_occured >= TRUNC(CURRENT_DATE - 30)
为什么?因为如果TRUNC(date_occured)晚于TRUNC(CURRENT_DATE-30),那么TRUNC(date_occured)之后的任何时间也必定晚于TRUNC(CURRENT_DATE-30)。
显然,date_occured> = TRUNC(date_occured)总是正确的(考虑一下)。
逻辑说,如果A> = B且B> = C,则得出A> = C
现在替换为: