如何在Oracle中仅根据日期部分比较两个DATE值?

发布于 2024-12-05 07:29:01 字数 657 浏览 3 评论 0原文

我尝试使用以下查询获取过去 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

兮子 2024-12-12 07:29:01

对于这种情况,您只需要截断右侧:

WHERE date_occured >= TRUNC(CURRENT_DATE - 30)

为什么?因为如果 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

现在替换:

  • A : date_occurred
  • B : TRUNC(date_occurred)
  • C : TRUNC(CURRENT_DATE - 30)

For this condition you only need to TRUNC the right-hand side:

WHERE date_occured >= TRUNC(CURRENT_DATE - 30)

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:

  • A : date_occured
  • B : TRUNC(date_occured)
  • C : TRUNC(CURRENT_DATE - 30)
£冰雨忧蓝° 2024-12-12 07:29:01

我想你也会想在选择部分中截断:

 SELECT TRUNC(date_occured) AS short_date_occured, COUNT(*)
 FROM problem 
 WHERE date_occured >= trunc(SYSDATE- 30) 
 GROUP BY short_date_occured;

I think you'll want to trunc in the select part too:

 SELECT TRUNC(date_occured) AS short_date_occured, COUNT(*)
 FROM problem 
 WHERE date_occured >= trunc(SYSDATE- 30) 
 GROUP BY short_date_occured;
余生一个溪 2024-12-12 07:29:01

尝试使用 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.

も让我眼熟你 2024-12-12 07:29:01

我倾向于怀疑托尼是正确的,并且您实际上只想TRUNC表达式的右侧。

如果您确实想要 TRUNC 表达式的两边并且遇到性能问题,则可能需要基于函数的索引,

CREATE INDEX idx_problem_trunc_dt_occured
    ON problem( trunc( date_occurred ) );

这将允许您的原始查询使用基于函数的索引。

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 index

CREATE INDEX idx_problem_trunc_dt_occured
    ON problem( trunc( date_occurred ) );

That would allow your original query to use the function-based index.

动次打次papapa 2024-12-12 07:29:01

这是索引友好的方法。

如果使用 Oracle 的本机 MONTHS_BETWEEN 函数,则不需要在列上使用函数。

它返回月数差异。天数也以差异形式给出,但在精度方面,这就是为什么我更喜欢使用 BETWEEN 子句

WHERE MONTHS_BETWEEN(date_occured, CURRENT_DATE - 30) BETWEEN 0 AND 1

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

WHERE MONTHS_BETWEEN(date_occured, CURRENT_DATE - 30) BETWEEN 0 AND 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文