Advertisement
YuvalGai

Untitled

Mar 27th, 2023 (edited)
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 7.40 KB | None | 0 0
  1. CREATE OR REPLACE TABLE CANDIVORE.SEMANTIC_LAYER.T_MATCHMAKING_DASHBOARD AS
  2.  
  3.  
  4. SELECT match_end_date, league, match_type,percentile,resource_sub_type,booster_name,mode(min_trophies_in_percentile) min_trophies_in_percentile, mode(max_trophies_in_percentile) max_trophies_in_percentile,round(mode(avg_trophies_in_percentile)) avg_trophies_in_percentile, mode(median_trophies_in_percentile) median_trophies_in_percentile,mode(unique_users_in_percentile) unique_users_in_percentile, round(mode(avg_trophy_gap)) avg_trophy_gap_in_match_type_per_percentile_group, SUM(is_won) wins, COUNT(*) matches, SUM(CASE WHEN rival_resource_sub_type = 'Special Edition' OR rival_resource_sub_type = 'Legendary' THEN 1 ELSE 0 END) matches_agianst_LESE, SUM(match_duration_sec) FROM (
  5. SELECT *
  6. , avg(trophy_gap) OVER (partition BY match_end_date, league, match_type ,percentile) avg_trophy_gap
  7. , MIN(trophies_cnt) OVER(partition BY match_end_date, league, percentile) min_trophies_in_percentile
  8. , MAX(trophies_cnt) OVER(partition BY match_end_date, league, percentile) max_trophies_in_percentile
  9. , median(trophies_cnt) OVER(partition BY match_end_date, league, percentile) median_trophies_in_percentile
  10. , avg(trophies_cnt) OVER(partition BY match_end_date, league, percentile) avg_trophies_in_percentile
  11. , COUNT(DISTINCT user_id) OVER(partition BY match_end_date, league, percentile) unique_users_in_percentile
  12. FROM (
  13.  
  14.  
  15. SELECT * FROM
  16. (SELECT A.*,B.LEAGUE,B.trophies_cnt,true_trophies_cnt,percentile, abs(true_trophies_cnt-rival_trophies_balance) trophy_gap FROM
  17.  
  18.  
  19. (SELECT match_end_date, user_id, match_id,match_type,is_won,rival_trophies_balance, trophies_cnt true_trophies_cnt,CASE
  20. WHEN booster_name = 'FoxyRoxySE' THEN 'Special Edition'
  21. WHEN booster_name = 'MonkeyJoojoo' THEN 'Legendary'
  22. WHEN booster_name = 'BroccoBoogie' THEN 'Legendary'
  23. WHEN booster_name = 'CleoCadabra' THEN 'Legendary'
  24. WHEN booster_name ='BillieBoom' THEN 'Legendary'
  25. WHEN booster_name ='CupidSE' THEN 'Special Edition'
  26. WHEN booster_name ='DoctorColorSE' THEN 'Special Edition'
  27. WHEN booster_name ='ElMagnetoSE' THEN 'Special Edition'
  28. WHEN booster_name ='AllAboardSE' THEN 'Special Edition'
  29. WHEN booster_name ='CupidSE' THEN 'Special Edition'
  30. WHEN booster_name ='AllAboard' THEN 'Diamond 2'
  31. WHEN booster_name ='AutoMatcher' THEN 'Gold'
  32. WHEN booster_name ='BalloonBlast' THEN 'Gold'
  33. WHEN booster_name ='BlazingBoard' THEN 'Gold'
  34. WHEN booster_name ='ColonelMcQuack' THEN 'Diamond 1'
  35. WHEN booster_name ='CrazyClovers' THEN 'Diamond 1'
  36. WHEN booster_name ='CrossAttack' THEN 'Gold'
  37. WHEN booster_name ='Painter' THEN 'Silver'
  38. WHEN booster_name ='UFOBeamSE' THEN 'Special Edition'
  39. WHEN booster_name ='Cupid' THEN 'Diamond 3'
  40. WHEN booster_name ='DoctorColor' THEN 'Diamond 3'
  41. WHEN booster_name ='ElMagneto' THEN 'Diamond 3'
  42. WHEN booster_name ='FoxyRoxy' THEN 'Diamond 2'
  43. WHEN booster_name ='JeevesBot' THEN 'Diamond 2'
  44. WHEN booster_name ='Lightsabers' THEN 'Diamond 1'
  45. WHEN booster_name ='Magic' THEN 'Gold'
  46. WHEN booster_name ='MrAppleberry' THEN 'Diamond 1'
  47. WHEN booster_name ='QueenCobra' THEN 'Diamond 2'
  48. WHEN booster_name ='SpecialDelivery' THEN 'Silver'
  49. WHEN booster_name ='SweepIt' THEN 'Gold'
  50. WHEN booster_name ='UFOBeam' THEN 'Diamond 2'
  51. WHEN booster_name ='WoollyWorkout' THEN 'Diamond 3'
  52. WHEN booster_name ='Infection' THEN 'Silver'
  53. WHEN booster_name ='RubberDucky' THEN 'Bronze'
  54. WHEN booster_name ='SmallBoom' THEN 'Bronze'
  55. WHEN booster_name ='CreateSpecials' THEN 'Silver'
  56. WHEN booster_name ='CheckmateCharles' THEN 'Diamond 3'
  57. ELSE 'bugged?'
  58. END resource_sub_type,CASE
  59. WHEN rival_booster_name = 'FoxyRoxySE' THEN 'Special Edition'
  60. WHEN rival_booster_name = 'MonkeyJoojoo' THEN 'Legendary'
  61. WHEN rival_booster_name = 'BroccoBoogie' THEN 'Legendary'
  62. WHEN rival_booster_name = 'CleoCadabra' THEN 'Legendary'
  63. WHEN rival_booster_name ='BillieBoom' THEN 'Legendary'
  64. WHEN rival_booster_name ='CupidSE' THEN 'Special Edition'
  65. WHEN rival_booster_name ='DoctorColorSE' THEN 'Special Edition'
  66. WHEN rival_booster_name ='ElMagnetoSE' THEN 'Special Edition'
  67. WHEN rival_booster_name ='AllAboardSE' THEN 'Special Edition'
  68. WHEN rival_booster_name ='CupidSE' THEN 'Special Edition'
  69. WHEN rival_booster_name ='AllAboard' THEN 'Diamond 2'
  70. WHEN rival_booster_name ='AutoMatcher' THEN 'Gold'
  71. WHEN rival_booster_name ='BalloonBlast' THEN 'Gold'
  72. WHEN rival_booster_name ='BlazingBoard' THEN 'Gold'
  73. WHEN rival_booster_name ='ColonelMcQuack' THEN 'Diamond 1'
  74. WHEN rival_booster_name ='CrazyClovers' THEN 'Diamond 1'
  75. WHEN rival_booster_name ='CrossAttack' THEN 'Gold'
  76. WHEN rival_booster_name ='Painter' THEN 'Silver'
  77. WHEN rival_booster_name ='UFOBeamSE' THEN 'Special Edition'
  78. WHEN rival_booster_name ='Cupid' THEN 'Diamond 3'
  79. WHEN rival_booster_name ='DoctorColor' THEN 'Diamond 3'
  80. WHEN rival_booster_name ='ElMagneto' THEN 'Diamond 3'
  81. WHEN rival_booster_name ='FoxyRoxy' THEN 'Diamond 2'
  82. WHEN rival_booster_name ='JeevesBot' THEN 'Diamond 2'
  83. WHEN rival_booster_name ='Lightsabers' THEN 'Diamond 1'
  84. WHEN rival_booster_name ='Magic' THEN 'Gold'
  85. WHEN rival_booster_name ='MrAppleberry' THEN 'Diamond 1'
  86. WHEN rival_booster_name ='QueenCobra' THEN 'Diamond 2'
  87. WHEN rival_booster_name ='SpecialDelivery' THEN 'Silver'
  88. WHEN rival_booster_name ='SweepIt' THEN 'Gold'
  89. WHEN rival_booster_name ='UFOBeam' THEN 'Diamond 2'
  90. WHEN rival_booster_name ='WoollyWorkout' THEN 'Diamond 3'
  91. WHEN rival_booster_name ='Infection' THEN 'Silver'
  92. WHEN rival_booster_name ='RubberDucky' THEN 'Bronze'
  93. WHEN rival_booster_name ='SmallBoom' THEN 'Bronze'
  94. WHEN rival_booster_name ='CreateSpecials' THEN 'Silver'
  95. WHEN rival_booster_name ='CheckmateCharles' THEN 'Diamond 3'
  96. ELSE 'bugged?'
  97. END rival_resource_sub_type, booster_name, match_duration_sec FROM candivore.prod.f_user_match WHERE match_end_date > '2022-10-12' AND pvp_mode = 'PvP' AND match_type != 'Challenge' AND (is_private IS NULL OR is_private = FALSE)) A
  98.  
  99. LEFT JOIN
  100.  
  101. (SELECT *, CASE
  102. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.1 THEN 'p0-p10'
  103. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.2 THEN 'p10-p20'
  104. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.3 THEN 'p20-p30'
  105. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.4 THEN 'p30-p40'
  106. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.5 THEN 'p40-p50'
  107. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.6 THEN 'p50-p60'
  108. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.7 THEN 'p60-p70'
  109. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.8 THEN 'p70-p80'
  110. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.9 THEN 'p80-p90'
  111. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.95 THEN 'p90-p95'
  112. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 0.99 THEN 'p95-p99'
  113. WHEN rrn/total_daily_matches_in_arena_group_and_match_type <= 1 THEN 'p99-p100' END percentile
  114. FROM
  115. (
  116. SELECT *,MAX(rrn) OVER (partition BY interval_date, league) total_daily_matches_in_arena_group_and_match_type
  117. FROM
  118. (
  119. (SELECT interval_date,user_id,trophies_cnt,CASE
  120.     WHEN login_arena<=13 THEN 'Studios'
  121.     WHEN login_arena<=22 THEN 'Master_League'
  122.     WHEN login_arena>=23 THEN 'Legends_League' END league
  123. ,ROW_NUMBER() OVER(partition BY interval_date, league ORDER BY trophies_cnt) rrn
  124. FROM CANDIVORE.PROD.DAILY_USERS_FROM_PARAMS WHERE is_active = 1 AND interval_date > '2022-09-12')
  125. )
  126. )
  127. ) B
  128.  
  129. ON A.user_id = B.user_id AND A.match_end_date = B.interval_date)
  130. )
  131.  
  132.  
  133. ) GROUP BY 1,2,3,4,5,6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement