如何在Oracle中仅根据日期部分比较两个DATE值?
我尝试使用以下查询获取过去 30 天的计数 -
SELECT date_occured, COUNT(*) FROM problem
WHERE date_occured >= (CURRENT_DATE - 30)
GROUP BY date_occured;
//date_occured field is of type DATE.
基本上,在我的查询中,我尝试仅比较条件 date_occurred >= (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 值的日期部分?
I am trying to get counts for last 30 days with the following query -
SELECT date_occured, COUNT(*) FROM problem
WHERE date_occured >= (CURRENT_DATE - 30)
GROUP BY date_occured;
//date_occured field is of type DATE.
Basically, in my query I am trying to compare only the date part in the condition date_occured >= (CURRENT_DATE - 30)
, but it seems to compare the time too.
I tried the TRUNC as follows -
TRUNC(date_occured) >= TRUNC(CURRENT_DATE - 30)
But when run the query it never returns.
I also tried -
SELECT date_occured, COUNT(*) FROM problem
GROUP BY date_occured
HAVING TRUNC(date_occured) >= TRUNC(CURRENT_DATE - 30);
Again it never returns.
How can I compare only the date parts from two DATE values in Oracle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于这种情况,您只需要截断右侧:
为什么?因为如果 TRUNC(date_occurred) 晚于 TRUNC(CURRENT_DATE - 30),则 TRUNC(date_occurred) 之后的任何时刻也必然晚于 TRUNC(CURRENT_DATE - 30)。
显然 date_occurred >= TRUNC(date_occurred) 总是正确的(想一想)。
逻辑表明,如果 A >= B 且 B >= C,则 A >= C
现在替换:
For this condition you only need to TRUNC the right-hand side:
Why? Because if TRUNC(date_occured) is later than TRUNC(CURRENT_DATE - 30), then any moment in time after TRUNC(date_occured) is bound to be later than TRUNC(CURRENT_DATE - 30) too.
It is obviously always true that date_occured >= TRUNC(date_occured) (think about it).
Logic says that if A >= B and B >= C then it follows that A >= C
Now substitute:
我想你也会想在选择部分中截断:
I think you'll want to trunc in the select part too:
尝试使用 SYSDATE 与 CURRENT_DATE。 Sysdate 使用服务器的本地时间,其中 CURRENT_DATE 以客户端连接的本地时间返回服务器的当前日期/时间。
Try using SYSDATE vs CURRENT_DATE. Sysdate uses the server's local time where CURRENT_DATE returns current date/time for the server in the client's connection's local time.
我倾向于怀疑托尼是正确的,并且您实际上只想
TRUNC
表达式的右侧。如果您确实想要
TRUNC
表达式的两边并且遇到性能问题,则可能需要基于函数的索引,这将允许您的原始查询使用基于函数的索引。
I would tend to suspect that Tony is correct and that you really only want to
TRUNC
the right-hand side of the expression.If you do want to
TRUNC
both sides of the expression and you're encountering performance issues, you probably need a function based indexThat would allow your original query to use the function-based index.
这是索引友好的方法。
如果使用 Oracle 的本机 MONTHS_BETWEEN 函数,则不需要在列上使用函数。
它返回月数差异。天数也以差异形式给出,但在精度方面,这就是为什么我更喜欢使用 BETWEEN 子句
Here is the index friendly approach.
you don't need to use functions on columns if you use Oracle's Native MONTHS_BETWEEN function.
It returns difference in number of months. Days are also given as difference but on the precision side that is why I preferred to use BETWEEN clause