0% found this document useful (0 votes)
12 views3 pages

Examples: Avg/Mavg:: Select

This document contains examples of SQL queries that use window functions like AVG, SUM, RANK to analyze transactional data and return aggregated results. The queries calculate a moving average, cumulative sum, and rank customers based on transaction amounts over a date range.

Uploaded by

daffa540
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Examples: Avg/Mavg:: Select

This document contains examples of SQL queries that use window functions like AVG, SUM, RANK to analyze transactional data and return aggregated results. The queries calculate a moving average, cumulative sum, and rank customers based on transaction amounts over a date range.

Uploaded by

daffa540
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Examples:

AVG/MAVG:
select
nb_id,
customer_id,
nb_created_date,
tfc_nb_trans_type,nbc_cust_recover_amt_usd,
avg(nbc_cust_recover_amt_usd) over
( partition by customer_id
order by customer_id, nb_id
rows unbounded preceding
) as moving_avg
from pp_working.tld_negative_balance
where tfc_nb_trans_type='NB'
and zeroifnull(nbc_cust_recover_amt_usd)>0
nb_id
49995266
54202479
54203715
44276752
53507593
17543140
19290058
40729726
49455562
51295743
54057979
32920580
37548740
46974733

cust_id
1152922251513660228
1152922354524579626
1152922354524579626
1152925319132309123
1152925319132309123
1152925874154569405
1152925874154569405
1152925874154569405
1152925874154569405
1152925874154569405
1152925874154569405
1152926780480383035
1152926780480383035
1152926780480383035

date
06/27/2008
08/28/2008
08/28/2008
02/23/2008
08/15/2008
12/14/2005
01/13/2006
11/30/2007
06/13/2008
07/25/2008
08/26/2008
04/22/2007
09/05/2007
05/01/2008

nbc_cust_recover_amt_usd
41.20
24.94
7.34
50.00
20.50
41.23
59.15
16.07
5.09
6.84
42.25
4.40
184.31
37.54

Cumulative sum. SUM/CSUM


select
nb_id,
customer_id,
nb_created_date,
tfc_nb_trans_type,nbc_cust_recover_amt_usd,
case
when transaction_type='J' and transaction_status_reason='O'
then 'CO'
else null
end as charge_off_flag,
sum(nbc_cust_recover_amt_usd) over
( partition by customer_id
order by customer_id, nb_id
rows between unbounded preceding and current row
) as nbc_accum_cust_recover_amt_usd
from pp_working.tld_negative_balance

moving avg
41.20
24.94
16.14
50.00
35.25
41.23
50.19
38.82
30.39
25.68
28.44
4.40
94.36
75.42

where tfc_nb_trans_type='NB'
and zeroifnull(nbc_cust_recover_amt_usd)>0

nb_id
49995266
54202479
54203715
44276752
53507593
17543140
19290058
40729726
49455562
51295743
54057979
32920580
37548740
46974733

customer_id
1152922251513660228
1152922354524579626
1152922354524579626
1152925319132309123
1152925319132309123
1152925874154569405
1152925874154569405
1152925874154569405
1152925874154569405
1152925874154569405
1152925874154569405
1152926780480383035
1152926780480383035
1152926780480383035

nb_trans_type
NB
NB
NB
NB
NB
NB
NB
NB
NB
NB
NB
NB
NB
NB

nbc_cust_recover_amt_usd
41.20
24.94
7.34
50.00
20.50
41.23
59.15
16.07
5.09
6.84
42.25
4.40
184.31
37.54

cumalative
sum
41.20
24.94
32.27
50.00
70.50
41.23
100.38
116.45
121.54
128.38
170.63
4.40
188.71
226.25

Moving sum:

RANK:
sel customer_id,balance_effective_date,total_sent_amt,
rank() over(partition by balance_effective_date order by total_sent_amt desc)
from pp_tables.dw_customer_balance
where balance_effective_date between '2008-09-10'
and
'2008-09-14'
qualify rank() over (partition by balance_effective_date order by
total_sent_amt desc) <=3
lists the top three customers based on the total_sent_amt for the last 4 days.

CUSTOMER_ID
1807652182419924599
2135855002470314633
1699651022709293431
1617355765112107445
2305761808593932081
2145933045741361039
1188144727781262762
1942085570436270323
2302295222056556685
1857672398721254653
1590025064260363174
2202978487789420616
2091708830516382809
1242887958792254209
1158835894645582910

Count:

Max:

Min:

BALANCE_EFFECTIVE_DATE
09/10/2008
09/10/2008
09/10/2008
09/11/2008
09/11/2008
09/11/2008
09/12/2008
09/12/2008
09/12/2008
09/13/2008
09/13/2008
09/13/2008
09/14/2008
09/14/2008
09/14/2008

TOTAL_SENT_AMT
36150000
30450000
29925000
30000000
20000000
20000000
43534000
36249200
30000000
18715400
14492500
14090000
725000
500000
254000

Rank(TOTAL_SENT_AMT)
1
2
3
1
2
2
1
2
3
1
2
3
1
2
3

You might also like