Guest User

Untitled

a guest
Jan 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. #standardSQL
  2. select COALESCE(pci_ytd.mrr_date, pci_qoq.mrr_date, pci_qtd.mrr_date,pci_mom.mrr_date, pci_mtd.mrr_date, pci_dod.mrr_date) as mrr_date,
  3. COALESCE(pci_ytd.crm_account_id, pci_qoq.crm_account_id, pci_qtd.crm_account_id,pci_mom.crm_account_id, pci_mtd.crm_account_id,pci_dod.crm_account_id) as crm_account_id,
  4. paid_crm_ind_qtd, paid_crm_ind_mtd,paid_crm_ind_ytd, paid_crm_ind_qoq, paid_crm_ind_mom,paid_crm_ind_dod
  5. from
  6. #YTD_returncustomers
  7. (
  8. select a.mrr_date, a.crm_account_id, case when sum(b.net_mrr_usd)>0 then 'Y' else 'N' end as paid_crm_ind_ytd
  9. from
  10. (
  11. select mrr_date, mrr_date_prior_yr_end, crm_account_id, sum(net_mrr_usd) net_mrr_usd, sum(net_mrr_usd_prior_yr_end) net_mrr_usd_prior_yr_end
  12. from edw_analyst_general.pop_mrr_granular_stage
  13. group by mrr_date, mrr_date_prior_yr_end, crm_account_id
  14. )a
  15. join
  16. (
  17. select mrr_date, crm_account_id, sum(net_mrr_usd) as net_mrr_usd from edw_analyst_general.pop_mrr_granular_stage
  18. group by mrr_date, crm_account_id
  19. )b on b.crm_account_id=a.crm_account_id
  20. where a.net_mrr_usd>0 and a.net_mrr_usd_prior_yr_end =0
  21. and b.mrr_date<a.mrr_date_prior_yr_end and b.mrr_date >= date_sub(a.mrr_date, interval 1 year)
  22. and a.crm_account_id ='0018000000xxTFjAAM'
  23. group by a.mrr_date, a.crm_account_id
  24. ) pci_ytd
  25. #QoQ_returncustomers
  26. LEFT JOIN
  27. (
  28. select a.mrr_date, a.crm_account_id, case when sum(b.net_mrr_usd)>0 then 'Y' else 'N' end as paid_crm_ind_qoq
  29. from
  30. (
  31. select mrr_date, mrr_date_same_day_prior_qtr, crm_account_id, sum(net_mrr_usd) net_mrr_usd, sum(net_mrr_usd_same_day_prior_qtr) net_mrr_usd_same_day_prior_qtr
  32. from edw_analyst_general.pop_mrr_granular_stage
  33. group by mrr_date, mrr_date_same_day_prior_qtr, crm_account_id
  34. )a
  35. join
  36. (
  37. select mrr_date, crm_account_id, sum(net_mrr_usd) as net_mrr_usd from edw_analyst_general.pop_mrr_granular_stage
  38. group by mrr_date, crm_account_id
  39. )b on b.crm_account_id=a.crm_account_id
  40. where a.net_mrr_usd>0 and a.net_mrr_usd_same_day_prior_qtr =0
  41. and b.mrr_date<a.mrr_date_same_day_prior_qtr and b.mrr_date >= date_sub(a.mrr_date, interval 1 year)
  42. and a.crm_account_id ='0018000000xxTFjAAM'
  43. group by a.mrr_date, a.crm_account_id
  44. ) pci_qoq
  45. ON pci_ytd.mrr_date=pci_qoq.mrr_date and pci_ytd.crm_account_id=pci_qoq.crm_account_id
  46. #qtd_returncustomers
  47. LEFT JOIN
  48. (
  49. select a.mrr_date, a.crm_account_id, case when sum(b.net_mrr_usd)>0 then 'Y' else 'N' end as paid_crm_ind_qtd
  50. from
  51. (
  52. select mrr_date, mrr_date_prior_qtr_end, crm_account_id, sum(net_mrr_usd) net_mrr_usd, sum(net_mrr_usd_prior_qtr_end) net_mrr_usd_prior_qtr_end
  53. from edw_analyst_general.pop_mrr_granular_stage
  54. group by mrr_date, mrr_date_prior_qtr_end, crm_account_id
  55. )a
  56. join
  57. (
  58. select mrr_date, crm_account_id, sum(net_mrr_usd) as net_mrr_usd from edw_analyst_general.pop_mrr_granular_stage
  59. group by mrr_date, crm_account_id
  60. )b on b.crm_account_id=a.crm_account_id
  61. where a.net_mrr_usd>0 and a.net_mrr_usd_prior_qtr_end =0
  62. and b.mrr_date<a.mrr_date_prior_qtr_end and b.mrr_date >= date_sub(a.mrr_date, interval 1 year)
  63. and a.crm_account_id ='0018000000xxTFjAAM'
  64. group by a.mrr_date, a.crm_account_id
  65. ) pci_qtd
  66. ON pci_qtd.mrr_date=pci_qoq.mrr_date and pci_qtd.crm_account_id=pci_qoq.crm_account_id
  67. #MoM_returncustomers
  68. LEFT JOIN
  69. (
  70. select a.mrr_date, a.crm_account_id, case when sum(b.net_mrr_usd)>0 then 'Y' else 'N' end as paid_crm_ind_mom
  71. from
  72. (
  73. select mrr_date, mrr_date_same_day_prior_mo, crm_account_id, sum(net_mrr_usd) net_mrr_usd, sum(net_mrr_usd_same_day_prior_mo) net_mrr_usd_same_day_prior_mo
  74. from edw_analyst_general.pop_mrr_granular_stage
  75. group by mrr_date, mrr_date_same_day_prior_mo, crm_account_id
  76. )a
  77. join
  78. (
  79. select mrr_date, crm_account_id, sum(net_mrr_usd) as net_mrr_usd from edw_analyst_general.pop_mrr_granular_stage
  80. group by mrr_date, crm_account_id
  81. )b on b.crm_account_id=a.crm_account_id
  82. where a.net_mrr_usd>0 and a.net_mrr_usd_same_day_prior_mo =0
  83. and b.mrr_date<a.mrr_date_same_day_prior_mo and b.mrr_date >= date_sub(a.mrr_date, interval 1 year)
  84. and a.crm_account_id ='0018000000xxTFjAAM'
  85. group by a.mrr_date, a.crm_account_id
  86. ) pci_mom
  87. ON pci_mom.mrr_date=pci_qtd.mrr_date and pci_mom.crm_account_id=pci_qtd.crm_account_id
  88. #mtd_returncustomers
  89. LEFT JOIN
  90. (
  91. select a.mrr_date, a.crm_account_id, case when sum(b.net_mrr_usd)>0 then 'Y' else 'N' end as paid_crm_ind_mtd
  92. from
  93. (
  94. select mrr_date, mrr_date_prior_mo_end, crm_account_id, sum(net_mrr_usd) net_mrr_usd, sum(net_mrr_usd_prior_mo_end) net_mrr_usd_prior_mo_end
  95. from edw_analyst_general.pop_mrr_granular_stage
  96. group by mrr_date, mrr_date_prior_mo_end, crm_account_id
  97. )a
  98. join
  99. (
  100. select mrr_date, crm_account_id, sum(net_mrr_usd) as net_mrr_usd from edw_analyst_general.pop_mrr_granular_stage
  101. group by mrr_date, crm_account_id
  102. )b on b.crm_account_id=a.crm_account_id
  103. where a.net_mrr_usd>0 and a.net_mrr_usd_prior_mo_end =0
  104. and b.mrr_date<a.mrr_date_prior_mo_end and b.mrr_date >= date_sub(a.mrr_date, interval 1 year)
  105. and a.crm_account_id ='0018000000xxTFjAAM'
  106. group by a.mrr_date, a.crm_account_id
  107. ) pci_mtd
  108. ON pci_mtd.mrr_date=pci_mom.mrr_date and pci_mtd.crm_account_id=pci_mom.crm_account_id
  109. #DoD_returncustomers
  110. LEFT JOIN
  111. (
  112. select a.mrr_date, a.crm_account_id, case when sum(b.net_mrr_usd)>0 then 'Y' else 'N' end as paid_crm_ind_dod
  113. from
  114. (
  115. select mrr_date, mrr_date_prior_day, crm_account_id, sum(net_mrr_usd) net_mrr_usd, sum(net_mrr_usd_prior_day) net_mrr_usd_prior_day
  116. from edw_analyst_general.pop_mrr_granular_stage
  117. group by mrr_date, mrr_date_prior_day, crm_account_id
  118. )a
  119. join
  120. (
  121. select mrr_date, crm_account_id, sum(net_mrr_usd) as net_mrr_usd from edw_analyst_general.pop_mrr_granular_stage
  122. group by mrr_date, crm_account_id
  123. )b on b.crm_account_id=a.crm_account_id
  124. where a.net_mrr_usd>0 and a.net_mrr_usd_prior_day =0
  125. and b.mrr_date<a.mrr_date_prior_day and b.mrr_date >= date_sub(a.mrr_date, interval 1 year)
  126. and a.crm_account_id ='0018000000xxTFjAAM'
  127. group by a.mrr_date, a.crm_account_id
  128. ) pci_dod
  129. ON pci_dod.mrr_date=pci_mtd.mrr_date and pci_dod.crm_account_id=pci_mtd.crm_account_id
  130. ORDER BY 1
Add Comment
Please, Sign In to add comment