Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare @today as date
- set @today = DATEADD(month, 0, GETDATE());
- declare @snapshot as nvarchar(7)
- set @snapshot = LEFT(@today, 4) + '' + RIGHT(LEFT(@today, 7), 2);
- declare @product as nvarchar(30)
- set @product = 'Pro';
-
- With GrossBookings as (
- SELECT
- SD.Date_Key
- , SD.Country_Key
- , STL.SubscriptionType
- , STL.SubscriptionChannel
- , 'Pro' as ProductName
- , SD.License_Key
- , C.CountryCode
- , SUM(SD.[M_SubscriptionDetail_GrossBookings] ) as GrossBookings
- FROM
- [EDW].[dbo].[Fact_SubscriptionDetail] SD
- join Product P on P.Product_Key = SD.Product_Key
- join Date D on D.Date_Key = SD.Date_Key
- join Country C on C.Country_Key = SD.Country_Key
- join SubscriptionTypeLostSeatsType STL on STL.SubscriptionTypeLostSeatsType_Key = SD.SubscriptionTypeLostSeatsType_Key
- WHERE
- P.ProductProductToName in ('Pro', <other products>)
- AND SD.SnapshotDate_Key like CONCAT(@snapshot, '%')
- GROUP BY
- SD.Date_Key
- , SD.Country_Key
- , STL.SubscriptionType
- , STL.SubscriptionChannel
- , SD.License_Key
- , C.CountryCode
- )
- , Daily_Numbers as (
- SELECT
- SD.Date_Key
- , C.CountryCode
- , STL.SubscriptionType
- , STL.SubscriptionChannel
- , P.ProductProductToName as ProductName
- , SUM(SD.[M_SubscriptionDetail_NewSeats] ) as NewSeats
- , SUM(SD.[M_SubscriptionDetail_WinbackSeats] ) as WinbackSeats
- , SUM(SD.[M_SubscriptionDetail_RenewedFlexSeats] ) as RenewedFlex
- , SUM(SD.[M_SubscriptionDetail_RenewedCommitSeats] ) as RenewedCommit
- , SUM(SD.[M_SubscriptionDetail_RenewedSeats] ) as RenewedSeats
- , SUM(SD.[M_SubscriptionDetail_LostNonPaymentSeats] ) as LostNonPayment
- , SUM(SD.[M_SubscriptionDetail_LostGraceInactiveSeats] ) as LostGraceInactive
- , SUM(SD.[M_SubscriptionDetail_LostOtherSeats] ) as LostOther
- , SUM(SD.[M_SubscriptionDetail_LostSeats] ) as LostSeats
- , CASE WHEN STL.SubscriptionType = 'COMMIT' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostCommit
- , CASE WHEN STL.SubscriptionType = 'FLEXI' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostFlex
- , SUM(SD.[M_SubscriptionDetail_ActiveFlexSeats] ) as ActiveFlex
- , SUM(SD.[M_SubscriptionDetail_ActiveCommitSeats] ) as ActiveCommit
- , SUM(SD.[M_SubscriptionDetail_ActiveSeats] ) as ActiveSeats
- , SUM(SD.[M_SubscriptionDetail_ActiveSeatsEndMonth] ) as ActiveEoM
- , SUM(GB.GrossBookings) as GrossBookings
- FROM
- [EDW].[dbo].[Fact_SubscriptionDetail] SD
- join Product P on P.Product_Key = SD.Product_Key
- join Country C on C.Country_Key = SD.Country_Key
- join SubscriptionTypeLostSeatsType STL on STL.SubscriptionTypeLostSeatsType_Key = SD.SubscriptionTypeLostSeatsType_Key
- 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
- WHERE
- SD.SnapshotDate_Key like CONCAT(@snapshot, '%')
- and P.ProductProductToName = @product
- GROUP BY
- SD.Date_Key
- , C.CountryCode
- , P.ProductProductToName
- , STL.SubscriptionType
- , STL.SubscriptionChannel
- )
- , FlexInfo as (
- SELECT
- D2.Date_Key
- , C.CountryCode
- , STL.SubscriptionType
- , STL.SubscriptionChannel
- , P.ProductProductToName
- , SUM(SD.[M_SubscriptionDetail_RenewedFlexSeats]) as RenewedFlex
- , SUM(SD.M_SubscriptionDetail_RenewedCommitSeats) as RenewedCommit
- , CASE WHEN STL.SubscriptionType = 'FLEXI' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostFlex
- , CASE WHEN STL.SubscriptionType = 'COMMIT' THEN SUM(SD.[M_SubscriptionDetail_LostSeats]) ELSE 0 END AS LostCommit
- , SUM(SD.M_SubscriptionDetail_WinbackSeats) as Winback
- , SUM(SD.M_SubscriptionDetail_NewSeats) as New
- FROM
- [EDW].[dbo].[Fact_SubscriptionDetail] SD
- JOIN Date D1 on D1.Date_Key = SD.Date_Key
- 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)))
- join Product P on P.Product_Key = SD.Product_Key
- join Country C on C.Country_Key = SD.Country_Key
- join SubscriptionTypeLostSeatsType STL on STL.SubscriptionTypeLostSeatsType_Key = SD.SubscriptionTypeLostSeatsType_Key
- WHERE
- SD.SnapshotDate_Key like CONCAT(@snapshot, '%')
- and P.ProductProductToName = @product
- GROUP BY
- D2.Date_Key
- , C.CountryCode
- , STL.SubscriptionType
- , STL.SubscriptionChannel
- , P.ProductProductToName
- )
- , Totals as (
- SELECT
- DN.Date_Key
- , DN.CountryCode
- , DN.SubscriptionChannel
- , DN.ProductName
- , SUM(DN.NewSeats ) as NewSeats
- , SUM(DN.WinbackSeats ) as WinbackSeats
- , SUM(DN.RenewedFlex ) as RenewedFlex
- , SUM(DN.RenewedCommit ) as RenewedCommit
- , SUM(DN.RenewedSeats ) as RenewedSeats
- , SUM(-DN.LostNonPayment) as LostNonPayment
- , SUM(-DN.LostGraceInactive) as LostGraceInactive
- , SUM(-DN.LostOther ) as LostOther
- , SUM(-DN.LostSeats ) as LostSeats
- , SUM(-DN.LostFlex ) as LostFlex
- , SUM(-DN.LostCommit ) as LostCommit
- , SUM(DN.GrossBookings ) as GrossBookings
- , SUM(DN.ActiveFlex) OVER (ORDER BY DN.Date_Key)
- - SUM(COALESCE(F.RenewedFlex, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(F.LostFlex, 0)) OVER (ORDER BY DN.Date_Key)
- - SUM(COALESCE(F.Winback, 0)) OVER (ORDER BY DN.Date_Key)
- as RunningFlex
- , SUM(DN.NewSeats) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(DN.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(-DN.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
- - (
- SUM(COALESCE(F.New, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(F.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(-F.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
- )
- as RunningCommit
- , SUM(DN.ActiveFlex) OVER (ORDER BY DN.Date_Key)
- - SUM(COALESCE(F.RenewedFlex, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(F.LostFlex, 0)) OVER (ORDER BY DN.Date_Key)
- - SUM(COALESCE(F.Winback, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(DN.NewSeats) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(DN.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(-DN.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
- - (
- SUM(COALESCE(F.New, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(F.RenewedCommit, 0)) OVER (ORDER BY DN.Date_Key)
- + SUM(COALESCE(-F.LostCommit, 0)) OVER (ORDER BY DN.Date_Key)
- )
- as RunningTotal
- FROM
- Daily_Numbers DN
- JOIN Date D on D.Date_Key = DN.Date_Key
- 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
- GROUP BY
- DN.Date_Key
- , DN.CountryCode
- , DN.SubscriptionChannel
- , DN.ProductName
- , DN.NewSeats
- , DN.ActiveFlex
- , DN.ActiveCommit
- , DN.ActiveSeats
- , DN.RenewedFlex
- , DN.RenewedCommit
- , DN.RenewedSeats
- , DN.LostFlex
- , DN.LostCommit
- , F.RenewedFlex
- , F.RenewedCommit
- , F.LostFlex
- , F.Winback
- , F.New
- , F.LostCommit
- )
-
- SELECT
- T.Date_Key
- , T.CountryCode
- , T.SubscriptionChannel
- , CAST('Seats' as nvarchar(100)) as ProductName
- , SUM(T.NewSeats ) as NewSeats
- , SUM(T.WinbackSeats ) as WinbackSeats
- , SUM(T.RenewedFlex ) as RenewedFlex
- , SUM(T.RenewedCommit ) as RenewedCommit
- , SUM(T.RenewedSeats ) as RenewedSeats
- , SUM(T.LostNonPayment ) as LostNonPayment
- , SUM(T.LostGraceInactive) as LostGraceInactive
- , SUM(T.LostOther ) as LostOther
- , SUM(T.LostSeats ) as LostSeats
- , SUM(T.LostFlex ) as LostFlex
- , SUM(T.LostCommit ) as LostCommit
- , SUM(T.GrossBookings ) as GrossBookings
- , T.RunningFlex
- , T.RunningCommit
- , T.RunningTotal
- FROM
- Totals T
- GROUP BY
- T.Date_Key
- , T.CountryCode
- , T.SubscriptionChannel
- , T.RunningFlex
- , T.RunningCommit
- , T.RunningTotal
- ORDER BY
- T.Date_Key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement