How to compare two DATE values based only on date part in Oracle?

后端 未结 5 2302
醉话见心
醉话见心 2021-02-19 18:53

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)
GRO         


        
5条回答
  •  既然无缘
    2021-02-19 19:38

    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)

提交回复
热议问题