query78.sql 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. -- start query 1 in stream 0 using template query78.tpl and seed 1819994127
  2. with ws as
  3. (select d_year AS ws_sold_year, ws_item_sk,
  4. ws_bill_customer_sk ws_customer_sk,
  5. sum(ws_quantity) ws_qty,
  6. sum(ws_wholesale_cost) ws_wc,
  7. sum(ws_sales_price) ws_sp
  8. from web_sales
  9. left join web_returns on wr_order_number=ws_order_number and ws_item_sk=wr_item_sk
  10. join date_dim on ws_sold_date_sk = d_date_sk
  11. where wr_order_number is null
  12. group by d_year, ws_item_sk, ws_bill_customer_sk
  13. ),
  14. cs as
  15. (select d_year AS cs_sold_year, cs_item_sk,
  16. cs_bill_customer_sk cs_customer_sk,
  17. sum(cs_quantity) cs_qty,
  18. sum(cs_wholesale_cost) cs_wc,
  19. sum(cs_sales_price) cs_sp
  20. from catalog_sales
  21. left join catalog_returns on cr_order_number=cs_order_number and cs_item_sk=cr_item_sk
  22. join date_dim on cs_sold_date_sk = d_date_sk
  23. where cr_order_number is null
  24. group by d_year, cs_item_sk, cs_bill_customer_sk
  25. ),
  26. ss as
  27. (select d_year AS ss_sold_year, ss_item_sk,
  28. ss_customer_sk,
  29. sum(ss_quantity) ss_qty,
  30. sum(ss_wholesale_cost) ss_wc,
  31. sum(ss_sales_price) ss_sp
  32. from store_sales
  33. left join store_returns on sr_ticket_number=ss_ticket_number and ss_item_sk=sr_item_sk
  34. join date_dim on ss_sold_date_sk = d_date_sk
  35. where sr_ticket_number is null
  36. group by d_year, ss_item_sk, ss_customer_sk
  37. )
  38. select
  39. ss_customer_sk,
  40. round(ss_qty/(coalesce(ws_qty,0)+coalesce(cs_qty,0)),2) ratio,
  41. ss_qty store_qty, ss_wc store_wholesale_cost, ss_sp store_sales_price,
  42. coalesce(ws_qty,0)+coalesce(cs_qty,0) other_chan_qty,
  43. coalesce(ws_wc,0)+coalesce(cs_wc,0) other_chan_wholesale_cost,
  44. coalesce(ws_sp,0)+coalesce(cs_sp,0) other_chan_sales_price
  45. from ss
  46. left join ws on (ws_sold_year=ss_sold_year and ws_item_sk=ss_item_sk and ws_customer_sk=ss_customer_sk)
  47. left join cs on (cs_sold_year=ss_sold_year and cs_item_sk=ss_item_sk and cs_customer_sk=ss_customer_sk)
  48. where (coalesce(ws_qty,0)>0 or coalesce(cs_qty, 0)>0) and ss_sold_year=2001
  49. order by
  50. ss_customer_sk,
  51. ss_qty desc, ss_wc desc, ss_sp desc,
  52. other_chan_qty,
  53. other_chan_wholesale_cost,
  54. other_chan_sales_price,
  55. ratio
  56. limit 100;
  57. -- end query 1 in stream 0 using template query78.tpl