In the following codes, how do you exclude members's spending that's larger than $500 for each year (instead of total spending for all years)?
select
Year
,month
,memberkey
,sum(spending) as spending
from table1
group by
1,2,3
In the following codes, how do you exclude members's spending that's larger than $500 for each year (instead of total spending for all years)?
select
Year
,month
,memberkey
,sum(spending) as spending
from table1
group by
1,2,3
A HAVING clause won't work here since you really want to aggregate at the YEAR
level to determine which records should be included. Traditionally you would do this with a correlated subquery, but in Teradata you can make use of the QUALIFY
clause:
SELECT "Year"
,"Month"
,MemberKey
,spending
from table1
QUALIFY sum(spending) OVER (PARTITION BY "Year", MemberID) < 500