Moosa
Moosa

Reputation: 3216

SQL Rolling 13 months in Teradata

I'm trying the below query to get the last 13 rolling months from a certain table. I keep getting the error 'expected something between the where keyword and the year keyword'. Seems like it's something basic but I can't seem to figure it out. I tried putting parentheses too but it still gives me an error.

select  count(*)
from t
where year(creat_dt) * 100 + month(creat_dt) BETWEEN trunc(add_months(current_date,-13),'MM') AND last_day(current_date,'MM'))

Upvotes: 0

Views: 3028

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269973

You are comparing an integer to a date. How about just using dates? If you have no future data, then this should be sufficient:

select  count(*)
from t
where creat_dt >=  trunc(add_months(current_date, -13), 'MM') 

Upvotes: 2

Related Questions