Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  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. ,c_preferred_cust_flag customer_preferred_cust_flag
  6. ,c_birth_country customer_birth_country
  7. ,c_login customer_login
  8. ,c_email_address customer_email_address
  9. ,d_year dyear
  10. ,sum(((ss_ext_list_price-ss_ext_wholesale_cost-ss_ext_discount_amt)+ss_ext_sales_price)/2) year_total
  11. ,'s' sale_type
  12. from customer
  13. ,store_sales
  14. ,date_dim
  15. where c_customer_sk = ss_customer_sk
  16. and ss_sold_date_sk = d_date_sk
  17. group by c_customer_id
  18. ,c_first_name
  19. ,c_last_name
  20. ,c_preferred_cust_flag
  21. ,c_birth_country
  22. ,c_login
  23. ,c_email_address
  24. ,d_year
  25. union all
  26. select c_customer_id customer_id
  27. ,c_first_name customer_first_name
  28. ,c_last_name customer_last_name
  29. ,c_preferred_cust_flag customer_preferred_cust_flag
  30. ,c_birth_country customer_birth_country
  31. ,c_login customer_login
  32. ,c_email_address customer_email_address
  33. ,d_year dyear
  34. ,sum((((cs_ext_list_price-cs_ext_wholesale_cost-cs_ext_discount_amt)+cs_ext_sales_price)/2) ) year_total
  35. ,'c' sale_type
  36. from customer
  37. ,catalog_sales
  38. ,date_dim
  39. where c_customer_sk = cs_bill_customer_sk
  40. and cs_sold_date_sk = d_date_sk
  41. group by c_customer_id
  42. ,c_first_name
  43. ,c_last_name
  44. ,c_preferred_cust_flag
  45. ,c_birth_country
  46. ,c_login
  47. ,c_email_address
  48. ,d_year
  49. union all
  50. select c_customer_id customer_id
  51. ,c_first_name customer_first_name
  52. ,c_last_name customer_last_name
  53. ,c_preferred_cust_flag customer_preferred_cust_flag
  54. ,c_birth_country customer_birth_country
  55. ,c_login customer_login
  56. ,c_email_address customer_email_address
  57. ,d_year dyear
  58. ,sum((((ws_ext_list_price-ws_ext_wholesale_cost-ws_ext_discount_amt)+ws_ext_sales_price)/2) ) year_total
  59. ,'w' sale_type
  60. from customer
  61. ,web_sales
  62. ,date_dim
  63. where c_customer_sk = ws_bill_customer_sk
  64. and ws_sold_date_sk = d_date_sk
  65. group by c_customer_id
  66. ,c_first_name
  67. ,c_last_name
  68. ,c_preferred_cust_flag
  69. ,c_birth_country
  70. ,c_login
  71. ,c_email_address
  72. ,d_year
  73. )
  74. select
  75. t_s_secyear.customer_id
  76. ,t_s_secyear.customer_first_name
  77. ,t_s_secyear.customer_last_name
  78. ,t_s_secyear.customer_email_address
  79. from year_total t_s_firstyear
  80. ,year_total t_s_secyear
  81. ,year_total t_c_firstyear
  82. ,year_total t_c_secyear
  83. ,year_total t_w_firstyear
  84. ,year_total t_w_secyear
  85. where t_s_secyear.customer_id = t_s_firstyear.customer_id
  86. and t_s_firstyear.customer_id = t_c_secyear.customer_id
  87. and t_s_firstyear.customer_id = t_c_firstyear.customer_id
  88. and t_s_firstyear.customer_id = t_w_firstyear.customer_id
  89. and t_s_firstyear.customer_id = t_w_secyear.customer_id
  90. and t_s_firstyear.sale_type = 's'
  91. and t_c_firstyear.sale_type = 'c'
  92. and t_w_firstyear.sale_type = 'w'
  93. and t_s_secyear.sale_type = 's'
  94. and t_c_secyear.sale_type = 'c'
  95. and t_w_secyear.sale_type = 'w'
  96. and t_s_firstyear.dyear = 1998
  97. and t_s_secyear.dyear = 1998+1
  98. and t_c_firstyear.dyear = 1998
  99. and t_c_secyear.dyear = 1998+1
  100. and t_w_firstyear.dyear = 1998
  101. and t_w_secyear.dyear = 1998+1
  102. and t_s_firstyear.year_total > 0
  103. and t_c_firstyear.year_total > 0
  104. and t_w_firstyear.year_total > 0
  105. and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_firstyear.year_total else null end
  106. > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
  107. and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_firstyear.year_total else null end
  108. > case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
  109. order by t_s_secyear.customer_id
  110. ,t_s_secyear.customer_first_name
  111. ,t_s_secyear.customer_last_name
  112. ,t_s_secyear.customer_email_address
  113. limit 100;
  114.  
  115.  
  116. ---------------------------------
  117.  
  118. Table definition os xx_sales are almost the same:
  119.  
  120. Append-Only Columnar Table "tpcds.store_sales"
  121. Column | Type | Modifiers | Storage | Stats target | Compression Type | Compression Level | Block Size | Description
  122. -----------------------+--------------+-----------+---------+--------------+------------------+-------------------+------------+-------------
  123. ss_sold_date_sk | integer | | plain | | zstd | 1 | 32768 |
  124. ss_sold_time_sk | integer | | plain | | zstd | 1 | 32768 |
  125. ss_item_sk | integer | not null | plain | | zstd | 1 | 32768 |
  126. ss_customer_sk | integer | | plain | | zstd | 1 | 32768 |
  127. ss_cdemo_sk | integer | | plain | | zstd | 1 | 32768 |
  128. ss_hdemo_sk | integer | | plain | | zstd | 1 | 32768 |
  129. ss_addr_sk | integer | | plain | | zstd | 1 | 32768 |
  130. ss_store_sk | integer | | plain | | zstd | 1 | 32768 |
  131. ss_promo_sk | integer | | plain | | zstd | 1 | 32768 |
  132. ss_ticket_number | bigint | not null | plain | | zstd | 1 | 32768 |
  133. ss_quantity | integer | | plain | | zstd | 1 | 32768 |
  134. ss_wholesale_cost | numeric(7,2) | | main | | zstd | 1 | 32768 |
  135. ss_list_price | numeric(7,2) | | main | | zstd | 1 | 32768 |
  136. ss_sales_price | numeric(7,2) | | main | | zstd | 1 | 32768 |
  137. ss_ext_discount_amt | numeric(7,2) | | main | | zstd | 1 | 32768 |
  138. ss_ext_sales_price | numeric(7,2) | | main | | zstd | 1 | 32768 |
  139. ss_ext_wholesale_cost | numeric(7,2) | | main | | zstd | 1 | 32768 |
  140. ss_ext_list_price | numeric(7,2) | | main | | zstd | 1 | 32768 |
  141. ss_ext_tax | numeric(7,2) | | main | | zstd | 1 | 32768 |
  142. ss_coupon_amt | numeric(7,2) | | main | | zstd | 1 | 32768 |
  143. ss_net_paid | numeric(7,2) | | main | | zstd | 1 | 32768 |
  144. ss_net_paid_inc_tax | numeric(7,2) | | main | | zstd | 1 | 32768 |
  145. ss_net_profit | numeric(7,2) | | main | | zstd | 1 | 32768 |
  146. Checksum: t
  147. Child tables: store_sales_1_prt_10,
  148. .........
  149. store_sales_1_prt_100,
  150. store_sales_1_prt_others
  151. Distributed by: (ss_item_sk, ss_ticket_number)
  152. Partition by: (ss_sold_date_sk)
  153. Options: appendonly=true, orientation=column, compresstype=zstd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement