确定单个日期范围重叠的四个日期范围中的哪一个

时间:2017-07-26 16:45:53

标签: sql-server tsql date count

我的订阅可以持续到任何地方(就我的输出而言应该关注)从1到4个财政季度(也可以看作是四个日期范围)

我正在使用亚利桑那州财政部:

  • Q1:7月1日 - 9月30日
  • Q2:10月1日 - 12月31日
  • Q3:1月1日 - 3月31日
  • Q4:4月1日 - 6月30日

根据订阅的开始和结束日期,我需要弄清楚每个季度有多少订阅活动。

例如,使用(YYYY-MM-DD),我有:

  • 从2016-07-06开始并在2017-02-22结束的订阅我应该可以看到我在Q1,Q2和Q3中有订阅。
  • 2016-10-18开始并于2016-10-24结束的另一个订阅只会被视为第二季度的活跃
  • 最后,订阅从2016-09-28开始但 没有 结束日期将被视为有效的Q1,Q2,Q3和Q4(所以无论从Q4开始的一个季度开始)

以下是我当前的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 |

1 个答案:

答案 0 :(得分:2)

一旦你有一张描述宿舍开始和结束日期的表格,就像@Sean Lange所建议的那样,你只需要将你的订阅的开始和结束日期与季度的开始和结束日期进行比较,然后将它们计算在重叠的位置。 。基本上,如果A.Start&lt;两个范围有一些重叠。 B.End和A.End&gt; B.Start。

这是模式的完整示例。

-------------------------
-- construct the quarters table (taken from the SQL Fiddle in comments above)

DECLARE @FiscalDate Table
(
  Id INT NOT NULL,
  FiscalYear INT NOT NULL,
  FiscalQuarter INT NOT NULL,
  QuarterStartDate DATE NOT NULL,
  QuarterEndDate DATE NOT NULL
  PRIMARY KEY(Id)
);

INSERT INTO @FiscalDate 
  (Id, FiscalYear, FiscalQuarter, QuarterStartDate, QuarterEndDate)
VALUES
  ( 1, 2016, 1, '2016-07-01', '2016-09-30')
  ,(2, 2016, 2, '2016-10-01', '2016-12-31')
  ,(3, 2017, 3, '2017-01-01', '2017-03-31')
  ,(4, 2017, 4, '2017-04-01', '2017-06-30')
;


-------------------------
--Get some random test data to imitate subscriptions ranges

DECLARE @tempSet Table(a date, b date)
INSERT INTO @tempSet SELECT TOP 15
    dateadd(dd, ROUND(365 * RAND(convert(varbinary, newid())), 0), '2016-07-01') as a
    , dateadd(dd, ROUND(365 * RAND(convert(varbinary, newid())), 0), '2016-07-01') as b
FROM sysobjects

-------------------------
-- Fix our random data a little (start date needs to be before end date)

DECLARE @DateRanges Table(StartDate date, EndDate date)
INSERT INTO @DateRanges
SELECT a, b FROM @tempset WHERE a <= b UNION SELECT b, a FROM @tempset WHERE b < a


-------------------------
-- Show our Date ranges in a useful order for review
SELECT * FROM @DateRanges ORDER BY StartDate, EndDate

-------------------------
-- Show our by-quarter counts.


SELECT
    fd.FiscalQuarter
    , count(*) as ActiveSubsCount
FROM
    @FiscalDate fd

    JOIN

    @DateRanges dr
        on fd.QuarterStartDate < dr.EndDate
        and fd.QuarterEndDate >= dr.StartDate
GROUP BY
    fd.FiscalQuarter


-------------------------
-- and in your desired output (without your quarters table)
SELECT
    COUNT(CASE WHEN '2016-07-01' < dr.EndDate AND '2016-09-30' >= dr.StartDate THEN 1 ELSE NULL END) AS FirstQuarter 
    , COUNT(CASE WHEN '2016-10-01' < dr.EndDate AND '2016-12-31' >= dr.StartDate THEN 1 ELSE NULL END) AS SecondQuarter  
    , COUNT(CASE WHEN '2017-01-01' < dr.EndDate AND '2017-03-31' >= dr.StartDate THEN 1 ELSE NULL END) AS ThirdQuarter 
    , COUNT(CASE WHEN '2017-04-01' < dr.EndDate AND '2017-06-30' >= dr.StartDate THEN 1 ELSE NULL END) AS FourthQuarter 
FROM
    @DateRanges dr