I am trying to get last 3 months data from the OTO_HISTORICAL_TRACKING_DETAIL CTE that I have created.
OTO_START_DATE is in DATE format - 'YYYY-MM-DD'.
This query returns 0 results
SELECT*
FROM OTO_HISTORICAL_TRACKING_DETAIL
WHERE OTO_START_DATE BETWEEN ADD_MONTHS(CURRENT_DATE, -4)
AND ADD_MONTHS(CURRENT_DATE, -1)
However, when I use this query, I get results
SELECT *
FROM OTO_HISTORICAL_TRACKING_DETAIL
WHERE OTO_START_DATE BETWEEN ADD_MONTHS(CURRENT_DATE, -4)
AND CURRENT_DATE
So lets get some trival data to check the logic:
gives:
Lets workout what could be happening by looking at all the values:
Well that could be the problem; current_date is the exact "day" and thus a month earlier, which might not be what you want. Perhaps you want to have the start/end of those months at which point you will want to use
DATE_TRUNC('month', current_month)andLAST_DATElike:
Giving: