Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. WITH SubscriptionInfo AS
  2. (
  3. SELECT
  4. [Subscriptions].[Customer_Id]
  5. ,[DistributorTypes].[Name] AS [Distributor Type]
  6. ,[Customers].Zip_Id
  7. ,[Subscriptions].[UnsubscribeReason_Id]
  8. ,[Subscriptions].[Id] AS [Subscription ID]
  9. ,CONVERT(DATE, [Subscriptions].[StartDate]) AS [Subscription Start Date]
  10. ,CONVERT(DATE, [Subscriptions].[EndDate]) AS [Subscription End Date]
  11. ,[PriorityLevels].PriorityLevel AS [Priority Level]
  12. ,CONVERT(DATE, [SubscriptionPriorityLevels].StartDate) AS [Priority Level Start Date]
  13. ,CONVERT(DATE, [SubscriptionPriorityLevels].EndDate) AS [Priority Level End Date]
  14. ,[FundingSources].[Name] AS [Funding Source]
  15. ,CONVERT(DATE, [SubscriptionFundingSources].StartDate) AS [SubscriptionFundingSources Start Date]
  16. ,CONVERT(DATE, [SubscriptionFundingSources].EndDate) AS [SubscriptionFundingSources End Date]
  17. FROM [Subscriptions]
  18.  
  19. LEFT JOIN [SubscriptionPriorityLevels]
  20. ON [SubscriptionPriorityLevels].Subscription_Id = Subscriptions.Id
  21. LEFT JOIN [PriorityLevels]
  22. ON [PriorityLevels].Id = SubscriptionPriorityLevels.PriorityLevel_Id
  23. LEFT JOIN [SubscriptionFundingSources]
  24. ON [SubscriptionFundingSources].Subscription_Id = Subscriptions.Id
  25. LEFT JOIN [FundingSources]
  26. ON [FundingSources].Id = [SubscriptionFundingSources].FundingSource_Id
  27. LEFT JOIN [Customers]
  28. ON [Customers].Id = [Subscriptions].Customer_Id
  29. LEFT JOIN [DistributorTypes]
  30. ON [DistributorTypes].Id = Customers.DistributorType_Id
  31.  
  32. WHERE
  33. ([Subscriptions].StartDate >= '2016-07-01') -- Dummy dates, would later be parameters
  34. AND ([Subscriptions].EndDate <= '2017-06-30'
  35. OR [Subscriptions].EndDate IS NULL)
  36. AND ([PriorityLevels].PriorityLevel IN (2, 3)) -- Only care about these two levels
  37. AND ([Customers].DistributorType_Id = 1) -- Distributor Type: Number One Distrubition
  38. AND ([SubscriptionFundingSources].FundingSource_Id = 2) -- Funding Source: First Bank
  39. )
  40.  
  41. SELECT
  42. [SubscriptionInfo].Customer_Id
  43. ,[SubscriptionInfo].[Subscription ID]
  44. ,MAX([SubscriptionInfo].[Priority Level]) AS [Highest Priority Level]
  45. ,CASE -- Determine which fiscal quarter each Subscription Start Date belongs to
  46. WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (7, 8, 9) THEN 1 -- July, August, September
  47. WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (10, 11, 12) THEN 2 -- October, November, December
  48. WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
  49. ELSE 4 -- April, May, June
  50. END AS [Fiscal Quarter Start Date]
  51. ,CASE -- Determine which fiscal quarter each Subscription Start Date belongs to
  52. WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (7, 8, 9) THEN 1 -- July, August, September
  53. WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (10, 11, 12) THEN 2 -- October, November, December
  54. WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
  55. ELSE 4 -- April, May, June
  56. END AS [Fiscal Quarter End Date]
  57. FROM [SubscriptionInfo]
  58.  
  59.  
  60. GROUP BY
  61. [SubscriptionInfo].Customer_Id
  62. ,[SubscriptionInfo].[Subscription ID]
  63. ,CASE -- Group Subscription Start Date's into Fiscal Quarters
  64. WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (7, 8, 9) THEN 1 -- July, August, September
  65. WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (10, 11, 12) THEN 2 -- October, November, December
  66. WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
  67. ELSE 4 -- April, May, June
  68. END
  69. ,CASE -- Group Subscription End Date's into Fiscal Quarters
  70. WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (7, 8, 9) THEN 1 -- July, August, September
  71. WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (10, 11, 12) THEN 2 -- October, November, December
  72. WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
  73. ELSE 4 -- April, May, June
  74. END
  75.  
  76. ORDER BY
  77. [SubscriptionInfo].Customer_Id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement