Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with year_total as (
  2.  select c_customer_id customer_id
  3.        ,c_first_name customer_first_name
  4.        ,c_last_name customer_last_name
  5.        ,d_year as year
  6.        ,max(ss_net_paid) year_total
  7.        ,'s' sale_type
  8.  from customer
  9.      ,store_sales
  10.      ,date_dim
  11.  where c_customer_sk = ss_customer_sk
  12.    and ss_sold_date_sk = d_date_sk
  13.    and d_year in (2001,2001+1)
  14.  group by c_customer_id
  15.          ,c_first_name
  16.          ,c_last_name
  17.          ,d_year
  18.  union all
  19.  select c_customer_id customer_id
  20.        ,c_first_name customer_first_name
  21.        ,c_last_name customer_last_name
  22.        ,d_year as year
  23.        ,max(ws_net_paid) year_total
  24.        ,'w' sale_type
  25.  from customer
  26.      ,web_sales
  27.      ,date_dim
  28.  where c_customer_sk = ws_bill_customer_sk
  29.    and ws_sold_date_sk = d_date_sk
  30.    and d_year in (2001,2001+1)
  31.  group by c_customer_id
  32.          ,c_first_name
  33.          ,c_last_name
  34.          ,d_year
  35.          )
  36.   select
  37.         t_s_secyear.customer_id, t_s_secyear.customer_first_name, t_s_secyear.customer_last_name
  38.  from year_total t_s_firstyear
  39.      ,year_total t_s_secyear
  40.      ,year_total t_w_firstyear
  41.      ,year_total t_w_secyear
  42.  where t_s_secyear.customer_id = t_s_firstyear.customer_id
  43.          and t_s_firstyear.customer_id = t_w_secyear.customer_id
  44.          and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  45.          and t_s_firstyear.sale_type = 's'
  46.          and t_w_firstyear.sale_type = 'w'
  47.          and t_s_secyear.sale_type = 's'
  48.          and t_w_secyear.sale_type = 'w'
  49.          and t_s_firstyear.year = 2001
  50.          and t_s_secyear.year = 2001+1
  51.          and t_w_firstyear.year = 2001
  52.          and t_w_secyear.year = 2001+1
  53.          and t_s_firstyear.year_total > 0
  54.          and t_w_firstyear.year_total > 0
  55.          and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
  56.            > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
  57.  order by 2,1,3
  58. limit 100;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement