Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

SQL help - response (1) by dnoeth

$
0
0
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.


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>