query31.sql 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. with ss as
  2. (select ca_county,d_qoy, d_year,sum(ss_ext_sales_price) as store_sales
  3. from store_sales,date_dim,customer_address
  4. where ss_sold_date_sk = d_date_sk
  5. and ss_addr_sk=ca_address_sk
  6. group by ca_county,d_qoy, d_year),
  7. ws as
  8. (select ca_county,d_qoy, d_year,sum(ws_ext_sales_price) as web_sales
  9. from web_sales,date_dim,customer_address
  10. where ws_sold_date_sk = d_date_sk
  11. and ws_bill_addr_sk=ca_address_sk
  12. group by ca_county,d_qoy, d_year)
  13. select
  14. ss1.ca_county
  15. ,ss1.d_year
  16. ,ws2.web_sales/ws1.web_sales web_q1_q2_increase
  17. ,ss2.store_sales/ss1.store_sales store_q1_q2_increase
  18. ,ws3.web_sales/ws2.web_sales web_q2_q3_increase
  19. ,ss3.store_sales/ss2.store_sales store_q2_q3_increase
  20. from
  21. ss ss1
  22. ,ss ss2
  23. ,ss ss3
  24. ,ws ws1
  25. ,ws ws2
  26. ,ws ws3
  27. where
  28. ss1.d_qoy = 1
  29. and ss1.d_year = 1998
  30. and ss1.ca_county = ss2.ca_county
  31. and ss2.d_qoy = 2
  32. and ss2.d_year = 1998
  33. and ss2.ca_county = ss3.ca_county
  34. and ss3.d_qoy = 3
  35. and ss3.d_year = 1998
  36. and ss1.ca_county = ws1.ca_county
  37. and ws1.d_qoy = 1
  38. and ws1.d_year = 1998
  39. and ws1.ca_county = ws2.ca_county
  40. and ws2.d_qoy = 2
  41. and ws2.d_year = 1998
  42. and ws1.ca_county = ws3.ca_county
  43. and ws3.d_qoy = 3
  44. and ws3.d_year =1998
  45. and case when ws1.web_sales > 0 then ws2.web_sales/ws1.web_sales else null end
  46. > case when ss1.store_sales > 0 then ss2.store_sales/ss1.store_sales else null end
  47. and case when ws2.web_sales > 0 then ws3.web_sales/ws2.web_sales else null end
  48. > case when ss2.store_sales > 0 then ss3.store_sales/ss2.store_sales else null end
  49. order by web_q1_q2_increase;