sql - Query to find top customers in countries with greatest difference in from 2 months -
lets theres columns: customer_id, revenue, country, date
what query show top customers highest revenue growth nov dec?
what query show top 100 customers in each country has highest growth in revenue november in december?
calculating revenue growth can using window function lag()
:
select customer_id, revenue, date, country, revenue - lag(revenue,1,revenue) on (partition customer_id order date) growth turnover extract(month date) in (11,12)
lag(revenue,1,0.0)
return revenue previous row. if there no previous row, return current row's revenue. results in growth of 0 first row each customer.
now growth
column turns greatest-n-per-group problem typically solved using window functions. window functions can't nested in single query, need use 2 levels of nested derived tables:
select customer_id, revenue, date, country, diff_to_previous, dense_rank() on (order growth desc nulls last) rnk ( select customer_id, revenue, date, country revenue - lag(revenue,1,0.0) on (partition customer_id order date) growth turnover extract(month date) in (11,12) ) t1
this assigns rank each row based on growth. can't use rnk
alias directly in where
clause , that's why use additional level of derived table.
so final statement customer highest growth is:
select * ( select customer_id, revenue, date, country, growth, dense_rank() on (order growth desc) rnk ( select customer_id, revenue, date, country, revenue - lag(revenue,1,0.0) on (partition customer_id order date) growth turnover extract(month date) in (11,12) ) t1 ) t2 rnk = 1;
to 100 highest growth each country need change calculation of rnk
per country:
select * ( select customer_id, revenue, date, country, growth dense_rank() on (partition country order growth desc) rnk ( select customer_id, revenue, date, country, revenue - lag(revenue,1,0.0) on (partition customer_id order date) growth turnover extract(month date) in (11,12) ) t1 ) t2 rnk <= 100;
date
horrible name column. not because keyword more importantly not document column contains. "start date"? "end date"? "purchase date"? "due date"?
Comments
Post a Comment