Listing 5: Finding Purchasing Patterns
select decode(GROUPING(to_char(trans_date, 'YYYY')), 1, 'All
Years',to_char(trans_date, 'YYYY')) as Year,
decode(GROUPING(band),1, 'All Bands', band) as Band,
sum(sales_amt) as total_sales_amt
from
(select customer_id, NTILE(4) over(order by sum(sales_amt)) as band
from sales group by customer_id) a, sales b
where a.customer_id = b.customer_id
group by cube(to_char(trans_date, 'YYYY'), band);
YEAR BAND TOTAL_SALES_AMT
------- ------- ------------------------
1999 1 13506897
1999 2 10987237
1999 3 5629814
1999 4 3328976
1999 All Bands 33452924
2000 1 20987154
2000 2 9852395
2000 3 5680923
2000 4 2984109
2000 All Bands 39504581
All Years 1 34494051
All Years 2 20839632
All Years 3 11310737
All Years 4 6313085
All Years All Bands 72957505
|