q78.sql 2.4 KB

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