query60.sql 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. with ss as (
  2. select
  3. i_item_id,sum(ss_ext_sales_price) total_sales
  4. from
  5. store_sales,
  6. date_dim,
  7. customer_address,
  8. item
  9. where
  10. item.i_item_id in (select
  11. i.i_item_id
  12. from
  13. item i
  14. where i_category in ('Children'))
  15. and ss_item_sk = i_item_sk
  16. and ss_sold_date_sk = d_date_sk
  17. and d_year = 1999
  18. and d_moy = 9
  19. and ss_addr_sk = ca_address_sk
  20. and ca_gmt_offset = -6
  21. group by i_item_id),
  22. cs as (
  23. select
  24. i_item_id,sum(cs_ext_sales_price) total_sales
  25. from
  26. catalog_sales,
  27. date_dim,
  28. customer_address,
  29. item
  30. where
  31. item.i_item_id in (select
  32. i.i_item_id
  33. from
  34. item i
  35. where i_category in ('Children'))
  36. and cs_item_sk = i_item_sk
  37. and cs_sold_date_sk = d_date_sk
  38. and d_year = 1999
  39. and d_moy = 9
  40. and cs_bill_addr_sk = ca_address_sk
  41. and ca_gmt_offset = -6
  42. group by i_item_id),
  43. ws as (
  44. select
  45. i_item_id,sum(ws_ext_sales_price) total_sales
  46. from
  47. web_sales,
  48. date_dim,
  49. customer_address,
  50. item
  51. where
  52. item.i_item_id in (select
  53. i.i_item_id
  54. from
  55. item i
  56. where i_category in ('Children'))
  57. and ws_item_sk = i_item_sk
  58. and ws_sold_date_sk = d_date_sk
  59. and d_year = 1999
  60. and d_moy = 9
  61. and ws_bill_addr_sk = ca_address_sk
  62. and ca_gmt_offset = -6
  63. group by i_item_id)
  64. select
  65. i_item_id
  66. ,sum(total_sales) total_sales
  67. from (select * from ss
  68. union all
  69. select * from cs
  70. union all
  71. select * from ws) tmp1
  72. group by i_item_id
  73. order by i_item_id
  74. ,total_sales
  75. limit 100;