SQL 在 Teradata 中滚动 13 个月

SQL Rolling 13 months in Teradata

我正在尝试使用以下查询来获取某个 table 的最近 13 个滚动月。我一直收到错误 'expected something between the where keyword and the year keyword'。似乎这是基本的东西,但我似乎无法弄清楚。我也试过加上括号,但它仍然给我一个错误。

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

您正在比较一个整数和一个日期。只使用日期怎么样?如果您没有未来的数据,那么这应该足够了:

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