Reputation: 1167
Here is the situation :
select sysdate from dual ;
> 30-APR-19
SELECT
count(*)
FROM
event_log
WHERE
edate between to_date('04232019', 'MMDDYYYY') and to_date('04292019', 'MMDDYYYY') ;
> 1156
SELECT
count(*)
FROM
event_log
WHERE
edate between (sysdate-7) and (sysdate-1) ;
> 1276
This is an event log, rows are date-tagged (row counts for previous days are static).
My question - why aren't the counts the same? What am i missing? Is there some semantic difference between the two ranges?
Upvotes: 0
Views: 37
Reputation: 142705
SYSDATE
is a function that returns both date and time component. If you don't TRUNC
ate it, you'd get both:
SQL> select sysdate,
2 sysdate - 7 prev_week,
3 sysdate - 1 yesterday
4 from dual;
SYSDATE PREV_WEEK YESTERDAY
------------------- ------------------- -------------------
30.04.2019 23:13:14 23.04.2019 23:13:14 29.04.2019 23:13:14
SQL>
If you TRUNC
ate it, you'd get the midnight:
SQL> select trunc(sysdate) today,
2 trunc(sysdate - 7) prev_week,
3 trunc(sysdate - 1) yesterday
4 from dual;
TODAY PREV_WEEK YESTERDAY
------------------- ------------------- -------------------
30.04.2019 00:00:00 23.04.2019 00:00:00 29.04.2019 00:00:00
SQL>
Which means that your last condition should, probably, be
where edate between trunc(sysdate-7) and trunc(sysdate-1) ;
Upvotes: 2