Advertisement
Guest User

Untitled

a guest
Apr 19th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. declare @today as date
  2. set @today = DATEADD(month, 0, GETDATE());
  3. declare @snapshot as nvarchar(7)
  4. set @snapshot = LEFT(@today, 4) + '' + RIGHT(LEFT(@today, 7), 2);
  5. declare @product as nvarchar(30)
  6. set @product = 'Pro';
  7. With GrossBookings as (
  8. SELECT
  9. SD.Date_Key
  10. , SD.Country_Key
  11. , STL.SubscriptionType
  12. , STL.SubscriptionChannel
  13. , 'Pro' as ProductName
  14. , SD.License_Key
  15. , C.CountryCode
  16. , SUM(SD.[M_SubscriptionDetail_GrossBookings] ) as GrossBookings
  17. FROM
  18. [EDW].[dbo].[Fact_SubscriptionDetail] SD
  19. join Product P on P.Product_Key = SD.Product_Key
  20. join Date D on D.Date_Key = SD.Date_Key
  21. join Country C on C.Country_Key = SD.Country_Key
  22. join SubscriptionTypeLostSeatsType STL on STL.SubscriptionTypeLostSeatsType_Key = SD.SubscriptionTypeLostSeatsType_Key
  23. WHERE
  24. P.ProductProductToName in ('Pro', <other products>)
  25. AND SD.SnapshotDate_Key like CONCAT(@snapshot, '%')
  26. GROUP BY
  27. SD.Date_Key
  28. , SD.Country_Key
  29. , STL.SubscriptionType
  30. , STL.SubscriptionChannel
  31. , SD.License_Key
  32. , C.CountryCode
  33. )
  34. , Daily_Numbers as (
  35. SELECT
  36. SD.Date_Key
  37. , C.CountryCode
  38. , STL.SubscriptionType
  39. , STL.SubscriptionChannel
  40. , P.ProductProductToName as ProductName
  41. , SUM(SD.[M_SubscriptionDetail_NewSeats] ) as NewSeats
  42. , SUM(SD.[M_SubscriptionDetail_WinbackSeats] ) as WinbackSeats
  43. , SUM(SD.[M_SubscriptionDetail_RenewedFlexSeats] ) as RenewedFlex
  44. , SUM(SD.[M_SubscriptionDetail_RenewedCommitSeats] ) as RenewedCommit
  45. , SUM(SD.[M_SubscriptionDetail_RenewedSeats] ) as RenewedSeats
  46. , SUM(SD.[M_SubscriptionDetail_LostNonPaymentSeats] ) as LostNonPayment
  47. , SUM(SD.[M_SubscriptionDetail_LostGraceInactiveSeats] ) as LostGraceInactive
  48. , SUM(SD.[M_SubscriptionDetail_LostOtherSeats] ) as LostOther
  49. , SUM(SD.[M_SubscriptionDetail_LostSeats] ) as LostSeats
  50. , CASE WHEN STL.SubscriptionType = 'COMMIT' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostCommit
  51. , CASE WHEN STL.SubscriptionType = 'FLEXI' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostFlex
  52. , SUM(SD.[M_SubscriptionDetail_ActiveFlexSeats] ) as ActiveFlex
  53. , SUM(SD.[M_SubscriptionDetail_ActiveCommitSeats] ) as ActiveCommit
  54. , SUM(SD.[M_SubscriptionDetail_ActiveSeats] ) as ActiveSeats
  55. , SUM(SD.[M_SubscriptionDetail_ActiveSeatsEndMonth] ) as ActiveEoM
  56. , SUM(GB.GrossBookings) as GrossBookings
  57. FROM
  58. [EDW].[dbo].[Fact_SubscriptionDetail] SD
  59. join Product P on P.Product_Key = SD.Product_Key
  60. join Country C on C.Country_Key = SD.Country_Key
  61. join SubscriptionTypeLostSeatsType STL on STL.SubscriptionTypeLostSeatsType_Key = SD.SubscriptionTypeLostSeatsType_Key
  62. join GrossBookings GB on GB.Date_Key = SD.Date_Key and GB.Country_Key = SD.Country_Key and GB.ProductName = P.ProductProductToName and GB.License_Key = SD.License_Key
  63. WHERE
  64. SD.SnapshotDate_Key like CONCAT(@snapshot, '%')
  65. and P.ProductProductToName = @product
  66. GROUP BY
  67. SD.Date_Key
  68. , C.CountryCode
  69. , P.ProductProductToName
  70. , STL.SubscriptionType
  71. , STL.SubscriptionChannel
  72. )
  73. , FlexInfo as (
  74. SELECT
  75. D2.Date_Key
  76. , C.CountryCode
  77. , STL.SubscriptionType
  78. , STL.SubscriptionChannel
  79. , P.ProductProductToName
  80. , SUM(SD.[M_SubscriptionDetail_RenewedFlexSeats]) as RenewedFlex
  81. , SUM(SD.M_SubscriptionDetail_RenewedCommitSeats) as RenewedCommit
  82. , CASE WHEN STL.SubscriptionType = 'FLEXI' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostFlex
  83. , CASE WHEN STL.SubscriptionType = 'COMMIT' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostCommit
  84. , SUM(SD.M_SubscriptionDetail_WinbackSeats) as Winback
  85. , SUM(SD.M_SubscriptionDetail_NewSeats) as New
  86. FROM
  87. [EDW].[dbo].[Fact_SubscriptionDetail] SD
  88. JOIN Date D1 on D1.Date_Key = SD.Date_Key
  89. JOIN Date D2 on D2.Date_Key = YEAR(DATEADD(day, 1, EOMONTH(D1.Date))) * 10000 + MONTH(DATEADD(day, 1, EOMONTH(D1.Date))) * 100 + DAY(DATEADD(day, 1, EOMONTH(D1.Date)))
  90. join Product P on P.Product_Key = SD.Product_Key
  91. join Country C on C.Country_Key = SD.Country_Key
  92. join SubscriptionTypeLostSeatsType STL on STL.SubscriptionTypeLostSeatsType_Key = SD.SubscriptionTypeLostSeatsType_Key
  93. WHERE
  94. SD.SnapshotDate_Key like CONCAT(@snapshot, '%')
  95. and P.ProductProductToName = @product
  96. GROUP BY
  97. D2.Date_Key
  98. , C.CountryCode
  99. , STL.SubscriptionType
  100. , STL.SubscriptionChannel
  101. , P.ProductProductToName
  102. )
  103. , Totals as (
  104. SELECT
  105. DN.Date_Key
  106. , DN.CountryCode
  107. , DN.SubscriptionChannel
  108. , DN.ProductName
  109. , SUM(DN.NewSeats ) as NewSeats
  110. , SUM(DN.WinbackSeats ) as WinbackSeats
  111. , SUM(DN.RenewedFlex ) as RenewedFlex
  112. , SUM(DN.RenewedCommit ) as RenewedCommit
  113. , SUM(DN.RenewedSeats ) as RenewedSeats
  114. , SUM(-DN.LostNonPayment) as LostNonPayment
  115. , SUM(-DN.LostGraceInactive) as LostGraceInactive
  116. , SUM(-DN.LostOther ) as LostOther
  117. , SUM(-DN.LostSeats ) as LostSeats
  118. , SUM(-DN.LostFlex ) as LostFlex
  119. , SUM(-DN.LostCommit ) as LostCommit
  120. , SUM(DN.GrossBookings ) as GrossBookings
  121. , SUM(DN.ActiveFlex) OVER (ORDER BY DN.Date_Key)
  122. - SUM(COALESCE(F.RenewedFlex, 0)) OVER (ORDER BY DN.Date_Key)
  123. + SUM(COALESCE(F.LostFlex, 0)) OVER (ORDER BY DN.Date_Key)
  124. - SUM(COALESCE(F.Winback, 0)) OVER (ORDER BY DN.Date_Key)
  125. as RunningFlex
  126. , SUM(DN.NewSeats) OVER (ORDER BY DN.Date_Key)
  127. + SUM(COALESCE(DN.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
  128. + SUM(COALESCE(-DN.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
  129. - (
  130. SUM(COALESCE(F.New, 0)) OVER (ORDER BY DN.Date_Key)
  131. + SUM(COALESCE(F.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
  132. + SUM(COALESCE(-F.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
  133. )
  134. as RunningCommit
  135. , SUM(DN.ActiveFlex) OVER (ORDER BY DN.Date_Key)
  136. - SUM(COALESCE(F.RenewedFlex, 0)) OVER (ORDER BY DN.Date_Key)
  137. + SUM(COALESCE(F.LostFlex, 0)) OVER (ORDER BY DN.Date_Key)
  138. - SUM(COALESCE(F.Winback, 0)) OVER (ORDER BY DN.Date_Key)
  139. + SUM(DN.NewSeats) OVER (ORDER BY DN.Date_Key)
  140. + SUM(COALESCE(DN.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
  141. + SUM(COALESCE(-DN.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
  142. - (
  143. SUM(COALESCE(F.New, 0)) OVER (ORDER BY DN.Date_Key)
  144. + SUM(COALESCE(F.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
  145. + SUM(COALESCE(-F.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
  146. )
  147. as RunningTotal
  148. FROM
  149. Daily_Numbers DN
  150. JOIN Date D on D.Date_Key = DN.Date_Key
  151. LEFT JOIN FlexInfo F on F.Date_Key = DN.Date_Key and F.SubscriptionType = DN.SubscriptionType and F.SubscriptionChannel = DN.SubscriptionChannel and F.CountryCode = DN.CountryCode --and F.ProductProductToName = DN.ProductName
  152. GROUP BY
  153. DN.Date_Key
  154. , DN.CountryCode
  155. , DN.SubscriptionChannel
  156. , DN.ProductName
  157. , DN.NewSeats
  158. , DN.ActiveFlex
  159. , DN.ActiveCommit
  160. , DN.ActiveSeats
  161. , DN.RenewedFlex
  162. , DN.RenewedCommit
  163. , DN.RenewedSeats
  164. , DN.LostFlex
  165. , DN.LostCommit
  166. , F.RenewedFlex
  167. , F.RenewedCommit
  168. , F.LostFlex
  169. , F.Winback
  170. , F.New
  171. , F.LostCommit
  172. )
  173. SELECT
  174. T.Date_Key
  175. , T.CountryCode
  176. , T.SubscriptionChannel
  177. , CAST('Seats' as nvarchar(100)) as ProductName
  178. , SUM(T.NewSeats ) as NewSeats
  179. , SUM(T.WinbackSeats ) as WinbackSeats
  180. , SUM(T.RenewedFlex ) as RenewedFlex
  181. , SUM(T.RenewedCommit ) as RenewedCommit
  182. , SUM(T.RenewedSeats ) as RenewedSeats
  183. , SUM(T.LostNonPayment ) as LostNonPayment
  184. , SUM(T.LostGraceInactive) as LostGraceInactive
  185. , SUM(T.LostOther ) as LostOther
  186. , SUM(T.LostSeats ) as LostSeats
  187. , SUM(T.LostFlex ) as LostFlex
  188. , SUM(T.LostCommit ) as LostCommit
  189. , SUM(T.GrossBookings ) as GrossBookings
  190. , T.RunningFlex
  191. , T.RunningCommit
  192. , T.RunningTotal
  193. FROM
  194. Totals T
  195. GROUP BY
  196. T.Date_Key
  197. , T.CountryCode
  198. , T.SubscriptionChannel
  199. , T.RunningFlex
  200. , T.RunningCommit
  201. , T.RunningTotal
  202. ORDER BY
  203. T.Date_Key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement