Advertisement
YuvalGai

Untitled

Jun 12th, 2023
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. CREATE OR REPLACE TABLE CANDIVORE.SEMANTIC_LAYER.T_OFFERS_DASHBOARD AS
  2.  
  3. SELECT AA.*, BB.prize_raw, BB.occasions_bought, BB.unique_users_bought, BB.level, BB.Coin_price, BB.IAP_price, BB.video_price, BB.rev, BB.dollars_rev FROM
  4.  
  5. (SELECT
  6. offer_start_date
  7. ---
  8. , ifnull(ltv_group2,''n/a'') ltv_group
  9. , ifnull(platform,''n/a'') platform
  10. , ifnull(trophy_group2,''n/a'') trophy_group
  11. , ifnull(league2,''n/a'') league
  12. /*, ifnull(country,''n/a'') country
  13. , ifnull(language,''n/a'') language */
  14. ---
  15. , offer_id
  16. , template_id
  17. , offer_context
  18. ---
  19. , count(distinct user_id) unique_users_shown
  20. , count(*) occasions_shown
  21. FROM (
  22. SELECT A.*, B.ltv_group, B.platform , B.trophy_group , B.league, B.country, B.language
  23. ,first_value(B.ltv_group) over(partition by A.user_id,template_id order by B.interval_date) ltv_group2
  24. ,first_value(B.trophy_group) over(partition by A.user_id,template_id order by B.interval_date) trophy_group2
  25. ,first_value(B.league) over(partition by A.user_id,template_id order by B.interval_date) league2
  26. from(
  27. (select
  28. date(derived_tstamp) interval_date
  29. , uuid user_id
  30. , template_id
  31. , offer_context
  32. , ifnull(config_index,''0'') config_index
  33. , CASE when length(offer_id) != 8 then offer_id else ''0'' end offer_id --
  34. , ifnull(coin_price,0) coin_price --
  35. , ifnull(video_price,0) video_price --
  36. , ifnull(iap_price,0) iap_price --
  37. , min(interval_date) over(partition by template_id) offer_start_date
  38. from candivore.prod.f_special_offer_shown where date(derived_tstamp) >= ''2022-10-01'') A
  39.  
  40. LEFT JOIN
  41.  
  42. (select
  43. user_id
  44. , interval_date
  45. , country
  46. , language
  47. , platform
  48. , app_version
  49. , app_minor_version
  50. , ltv_group
  51. , date(first_install_dt) as install_date
  52. , CASE
  53. WHEN trophies_cnt < 125 then ''0-125''
  54. WHEN trophies_cnt < 1400 then ''125-1400''
  55. WHEN trophies_cnt < 2000 then ''1400-2000''
  56. WHEN trophies_cnt < 3800 then ''2000-3800''
  57. WHEN trophies_cnt < 7000 then ''3800-7000''
  58. WHEN trophies_cnt < 16000 then ''7000-16000''
  59. WHEN trophies_cnt < 30000 then ''16000-30000''
  60. WHEN trophies_cnt >= 30000 then ''30000+''
  61. END as trophy_group
  62. ,CASE
  63. WHEN trophies_cnt < 800 THEN ''800''
  64. WHEN trophies_cnt < 3800 THEN ''Studios''
  65. WHEN trophies_cnt < 30000 THEN ''Master_League''
  66. WHEN trophies_cnt >= 30000 THEN ''Legends_League''
  67. END league
  68. from candivore.prod.daily_users_from_params where interval_date >= ''2022-10-01'') B
  69.  
  70. on A.user_id = B.user_id and A.interval_date = B.interval_date
  71. )) GROUP BY 1,2,3,4,5,6,7,8) AA
  72.  
  73. left JOIN
  74.  
  75. (SELECT
  76. -- interval_date
  77. ---
  78.  
  79. ifnull(ltv_group2,''n/a'') ltv_group
  80. , ifnull(platform,''n/a'') platform
  81. , ifnull(trophy_group2,''n/a'') trophy_group
  82. , ifnull(league2,''n/a'') league
  83. /*, ifnull(country,''n/a'') country
  84. , ifnull(language,''n/a'') language*/
  85. ---
  86. , offer_id -- offer_id
  87. , template_id
  88. ---
  89. , prize_raw
  90. , coin_price
  91. , video_price
  92. , iap_price
  93. , level
  94. --, min(interval_date) over(partition by offer_id) offer_start_date
  95. , count(*) occasions_bought
  96. , count(distinct user_id) unique_users_bought
  97. , sum(iap_price) rev
  98. , sum(dollar_price) dollars_rev
  99. /*, sum(case when level = 1 then 1 else 0 end) bought_lvl1
  100. , sum(case when level = 2 then 1 else 0 end) bought_lvl2
  101. , sum(case when level = 3 then 1 else 0 end) bought_lvl3
  102. , sum(case when level = 4 then 1 else 0 end) bought_lvl4
  103. , sum(case when level = 5 then 1 else 0 end) bought_lvl5*/
  104. FROM (
  105. SELECT A.*, B.ltv_group, B.platform, B.trophy_group, B.league, B.country, B.language
  106. ,first_value(B.ltv_group) over(partition by A.user_id,template_id order by B.interval_date) ltv_group2
  107. ,first_value(B.trophy_group) over(partition by A.user_id,template_id order by B.interval_date) trophy_group2
  108. ,first_value(B.league) over(partition by A.user_id,template_id order by B.interval_date) league2
  109. from (
  110. (select
  111. date(derived_tstamp) interval_date
  112. , uuid user_id
  113. , ifnull(offer_context,''0'') offer_context
  114. , ifnull(purchase_type,''0'') purchase_type
  115. , ifnull(purchase_subtype,''0'') purchase_subtype
  116. , ifnull(prize_raw,''0'') prize_raw
  117. , ifnull(offer_index,''0'') offer_index
  118. , template_id --
  119. , level
  120. , CASE when length(shown_offer_id) != 8 then shown_offer_id else ''0'' end offer_id
  121. , ifnull(coin_price,0) coin_price
  122. , ifnull(video_price,0) video_price
  123. , ifnull(iap_price,0) iap_price
  124. , ifnull(dollar_price,0) dollar_price
  125. from candivore.prod.f_special_offer_bought) A
  126.  
  127. LEFT JOIN
  128.  
  129. (select
  130. user_id
  131. , interval_date
  132. , country
  133. , language
  134. , platform
  135. , app_version
  136. , app_minor_version
  137. , ltv_group
  138. , date(first_install_dt) as install_date
  139. , CASE
  140. WHEN trophies_cnt < 125 then ''0-125''
  141. WHEN trophies_cnt < 1400 then ''125-1400''
  142. WHEN trophies_cnt < 2000 then ''1400-2000''
  143. WHEN trophies_cnt < 3800 then ''2000-3800''
  144. WHEN trophies_cnt < 7000 then ''3800-7000''
  145. WHEN trophies_cnt < 16000 then ''7000-16000''
  146. WHEN trophies_cnt < 30000 then ''16000-30000''
  147. WHEN trophies_cnt >= 30000 then ''30000+''
  148. END as trophy_group
  149. ,CASE
  150. WHEN trophies_cnt < 800 THEN ''800''
  151. WHEN trophies_cnt < 3800 THEN ''Studios''
  152. WHEN trophies_cnt < 30000 THEN ''Master_League''
  153. WHEN trophies_cnt >= 30000 THEN ''Legends_League''
  154. END league
  155. from candivore.prod.daily_users_from_params where interval_date >= ''2022-10-01'') B
  156.  
  157. on A.user_id = B.user_id and A.interval_date = B.interval_date
  158. )) GROUP BY 1,2,3,4,5,6,7,8,9,10,11) BB
  159.  
  160. ON /*AA.offer_start_date = BB.offer_start_date and*/ AA.ltv_group = BB.ltv_group and AA.trophy_group = BB.trophy_group and AA.platform = BB.platform and AA.league = BB.league /*and AA.country = BB.country
  161. and AA.language = BB.language and AA.config_index = BB.offer_index*/ and AA.offer_id = BB.offer_id and AA.template_id = BB.template_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement