【发布时间】:2018-01-02 03:09:15
【问题描述】:
我的订阅可以持续任何时间(就我的输出而言),从 1 到 4 个财政季度(也可以视为四个日期范围)
我正在使用亚利桑那州财政季度:
- 第一季度:7 月 1 日 - 9 月 30 日
- 第二季度:10 月 1 日 - 12 月 31 日
- 第三季度:1 月 1 日 - 3 月 31 日
- 第四季度:4 月 1 日 - 6 月 30 日
我需要根据订阅的开始日期和结束日期计算每个季度有多少订阅处于活动状态。
例如,使用 (YYYY-MM-DD),我有:
- 从 2016 年 7 月 6 日开始到 2017 年 2 月 22 日结束的订阅我应该能够看到我在第一季度、第二季度和第三季度有一个有效的订阅。
- 从 2016 年 10 月 18 日开始到 2016 年 10 月 24 日结束的另一个订阅将仅被视为在第二季度有效
- 最后,从 2016 年 9 月 28 日开始但没有结束日期的订阅将被视为在第一季度、第二季度、第三季度和第四季度有效(因此无论从第一季度到第四季度)
下面是我当前的 SQL Server 脚本,这里是 SQL Fiddle:
WITH SubscriptionInfo AS
(
SELECT
[Subscriptions].[Customer_Id]
,[DistributorTypes].[Name] AS [Distributor Type]
,[Customers].Zip_Id
,[Subscriptions].[UnsubscribeReason_Id]
,[Subscriptions].[Id] AS [Subscription ID]
,CONVERT(DATE, [Subscriptions].[StartDate]) AS [Subscription Start Date]
,CONVERT(DATE, [Subscriptions].[EndDate]) AS [Subscription End Date]
,[PriorityLevels].PriorityLevel AS [Priority Level]
,CONVERT(DATE, [SubscriptionPriorityLevels].StartDate) AS [Priority Level Start Date]
,CONVERT(DATE, [SubscriptionPriorityLevels].EndDate) AS [Priority Level End Date]
,[FundingSources].[Name] AS [Funding Source]
,CONVERT(DATE, [SubscriptionFundingSources].StartDate) AS [SubscriptionFundingSources Start Date]
,CONVERT(DATE, [SubscriptionFundingSources].EndDate) AS [SubscriptionFundingSources End Date]
FROM [Subscriptions]
LEFT JOIN [SubscriptionPriorityLevels]
ON [SubscriptionPriorityLevels].Subscription_Id = Subscriptions.Id
LEFT JOIN [PriorityLevels]
ON [PriorityLevels].Id = SubscriptionPriorityLevels.PriorityLevel_Id
LEFT JOIN [SubscriptionFundingSources]
ON [SubscriptionFundingSources].Subscription_Id = Subscriptions.Id
LEFT JOIN [FundingSources]
ON [FundingSources].Id = [SubscriptionFundingSources].FundingSource_Id
LEFT JOIN [Customers]
ON [Customers].Id = [Subscriptions].Customer_Id
LEFT JOIN [DistributorTypes]
ON [DistributorTypes].Id = Customers.DistributorType_Id
WHERE
([Subscriptions].StartDate >= '2016-07-01') -- Dummy dates, would later be parameters
AND ([Subscriptions].EndDate <= '2017-06-30'
OR [Subscriptions].EndDate IS NULL)
AND ([PriorityLevels].PriorityLevel IN (2, 3)) -- Only care about these two levels
AND ([Customers].DistributorType_Id = 1) -- Distributor Type: Number One Distrubition
AND ([SubscriptionFundingSources].FundingSource_Id = 2) -- Funding Source: First Bank
)
SELECT
[SubscriptionInfo].Customer_Id
,[SubscriptionInfo].[Subscription ID]
,MAX([SubscriptionInfo].[Priority Level]) AS [Highest Priority Level]
,CASE -- Determine which fiscal quarter each Subscription Start Date belongs to
WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (7, 8, 9) THEN 1 -- July, August, September
WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (10, 11, 12) THEN 2 -- October, November, December
WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
ELSE 4 -- April, May, June
END AS [Fiscal Quarter Start Date]
,CASE -- Determine which fiscal quarter each Subscription Start Date belongs to
WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (7, 8, 9) THEN 1 -- July, August, September
WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (10, 11, 12) THEN 2 -- October, November, December
WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
ELSE 4 -- April, May, June
END AS [Fiscal Quarter End Date]
FROM [SubscriptionInfo]
GROUP BY
[SubscriptionInfo].Customer_Id
,[SubscriptionInfo].[Subscription ID]
,CASE -- Group Subscription Start Date's into Fiscal Quarters
WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (7, 8, 9) THEN 1 -- July, August, September
WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (10, 11, 12) THEN 2 -- October, November, December
WHEN MONTH([SubscriptionInfo].[Subscription Start Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
ELSE 4 -- April, May, June
END
,CASE -- Group Subscription End Date's into Fiscal Quarters
WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (7, 8, 9) THEN 1 -- July, August, September
WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (10, 11, 12) THEN 2 -- October, November, December
WHEN MONTH([SubscriptionInfo].[Subscription End Date]) IN (1, 2, 3) THEN 3 -- January, Feburary, March
ELSE 4 -- April, May, June
END
ORDER BY
[SubscriptionInfo].Customer_Id
到目前为止,我能够确定订阅开始于哪个财政季度以及结束于哪个财政季度。
我希望能够计算每个季度有多少订阅处于活动状态。
期望的输出:
| FirstQuarter | SecondQuarter | ThirdQuarter | FourthQuarter |
|--------------|---------------|--------------|---------------|
| 2 | 1 | 3 | 3 |
【问题讨论】:
-
为什么不创建一个包含 FiscalYear、QuarterNumber 和 StartDate 的 Quarters 表?然后,您可以轻松确定给定订阅涵盖哪些季度。
-
@SeanLange 原谅我,我不确定我是否在关注。像 SQL Fiddle 这样的东西是你的建议吗?你能详细说明我将如何使用它吗?
标签: sql-server tsql date count