SELECT card_number,
merchant,
EXTRACT(YEAR FROM trans_date) AS trans_year,
EXTRACT(MONTH FROM trans_date) AS trans_month,
trans_year * 12 + trans_month AS trans_ym,
SUM(SUM(amount))
OVER (PARTITION BY card_number, merchant
ORDER BY trans_ym
ROWS 2 PRECEDING) AS sumamt
FROM trans
GROUP BY 1,2,3
QUALIFY sumamt > 1000
AND trans_ym - MIN(trans_ym) OVER (PARTITION BY card_number, merchant
ORDER BY trans_ym
ROWS 2 PRECEDING) = 2
This should be what you want: calculate the amount per month/creditcard/merchant and then check for three consecutive months and a sum(amount) > 1000.
Might return multiple rows per credit card, one row for each three months period meeting your condition.
This should be what you want: calculate the amount per month/creditcard/merchant and then check for three consecutive months and a sum(amount) > 1000.
Might return multiple rows per credit card, one row for each three months period meeting your condition.