Listing 2: Finding Top 5 products of Elite Customers
select * from
(select product_id, sum(sales_amt) as product_sales,
RANK() over(order by sum(sales_amt) desc) as
product_rank
from sales
where customer_id in
(select customer_id from
(select customer_id,
100 * CUME_DIST() over(order by sum(sales_amt) asc) as
cust_percentile
from sales group by customer_id)
where cust_percentile > 85)
group by product_id)
where product_rank <= 5
order by product_rank;
PRODUCT_ID PRODUCT_SALES PRODUCT_RANK
----------------- --------------------- --------------------
SO-1278 3569871 1
SE-9087 2998109 2
SI-7652 2587397 3
SD-0386 2001298 4
CO-8925 1098763 5
|