SQL Server Joining on a reference table that contains fiscal years and quarters

k2fxgqgv  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(80)

I am running a SQL script that outputs results in the below format.
| fiscal_year | Quarter | first_service | ServiceStartDate | ServiceEndDate |
| ------------ | ------------ | ------------ | ------------ | ------------ |

The problem I am facing is how to join on my reference table (Ref_Dates given below) in such a way that the results only return a row for the first occurrence within a fiscal year rather than returning all the quarters the dates span.

I have two dates I am using to join the State_QStart & State_QEnd on: ServiceStartDate and ServiceEndDate. The main goal is to only grab the first service in a fiscal year (even if the the service dates span multiple fiscal years). Our clients are treated as newly served every October 1st, so if the ServcieStartDate is in Q4 of one fiscal year, and their ServiceEndDate is on or after Q1 of the following fiscal year, they are counted as served in Q4 and Q1. Right now my results contain duplicates because it is getting all the quarters within the service dates.

EXAMPLE: ServiceStartDate = 2020-03-14, ServiceEndDate = 2020-10-03

My Output:
| fiscal_year | Quarter | first_service | ServiceStartDate | ServiceEndDate |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 2020 | Q2 | 2020-03-14 | 2020-03-14 | 2020-10-03 |
| 2020 | Q3 | 2020-03-14 | 2020-03-14 | 2020-10-03 |
| 2020 | Q4 | 2020-03-14 | 2020-03-14 | 2020-10-03 |
| 2021 | Q1 | 2020-03-14 | 2020-03-14 | 2020-10-03 |

Expected Output:

fiscal_yearQuarterfirst_serviceServiceStartDateServiceEndDate
2020Q22020-03-142020-03-142020-10-03
2021Q12020-03-142020-03-142020-10-03

Tables: Ref_Dates: My code requires the "State_" columns not the "ANL_".

-- Create the Ref_Dates table
CREATE TABLE Ref_Dates (
    [Fiscal_Year] INT NULL,
    [ANL_FYStart] DATE NULL,
    [ANL_FYEnd] DATE NULL,
    [State_FYStart] DATE NULL,
    [State_FYEnd] DATE NULL,
    [Quarter] VARCHAR(2) NULL,
    [ANL_QStart] DATE NULL,
    [ANL_QEnd] DATE NULL,
    [State_QStart] DATE NULL,
    [State_QEnd] DATE NULL
);

-- Insert values into the Ref_Dates table
INSERT INTO Ref_Dates VALUES
(2020, '2019-07-01', '2020-06-30', '2019-10-01', '2020-09-30', 'Q1', '2019-07-01', '2019-09-30', '2019-10-01', '2019-12-31'),
(2020, '2019-07-01', '2020-06-30', '2019-10-01', '2020-09-30', 'Q2', '2019-10-01', '2019-12-31', '2020-01-01', '2020-03-31'),
(2020, '2019-07-01', '2020-06-30', '2019-10-01', '2020-09-30', 'Q3', '2020-01-01', '2020-03-31', '2020-04-01', '2020-06-30'),
(2020, '2019-07-01', '2020-06-30', '2019-10-01', '2020-09-30', 'Q4', '2020-04-01', '2020-06-30', '2020-07-01', '2020-09-30'),
(2021, '2020-07-01', '2021-06-30', '2020-10-01', '2021-09-30', 'Q1', '2020-07-01', '2020-09-30', '2020-10-01', '2020-12-31'),
(2021, '2020-07-01', '2021-06-30', '2020-10-01', '2021-09-30', 'Q2', '2020-10-01', '2020-12-31', '2021-01-01', '2021-03-31'),
(2021, '2020-07-01', '2021-06-30', '2020-10-01', '2021-09-30', 'Q3', '2021-01-01', '2021-03-31', '2021-04-01', '2021-06-30'),
(2021, '2020-07-01', '2021-06-30', '2020-10-01', '2021-09-30', 'Q4', '2021-04-01', '2021-06-30', '2021-07-01', '2021-09-30'),
(2022, '2021-07-01', '2022-06-30', '2021-10-01', '2022-09-30', 'Q1', '2021-07-01', '2021-09-30', '2021-10-01', '2021-12-31'),
(2022, '2021-07-01', '2022-06-30', '2021-10-01', '2022-09-30', 'Q2', '2021-10-01', '2021-12-31', '2022-01-01', '2022-03-31'),
(2022, '2021-07-01', '2022-06-30', '2021-10-01', '2022-09-30', 'Q3', '2022-01-01', '2022-03-31', '2022-04-01', '2022-06-30'),
(2022, '2021-07-01', '2022-06-30', '2021-10-01', '2022-09-30', 'Q4', '2022-04-01', '2022-06-30', '2022-07-01', '2022-09-30'),
(2023, '2022-07-01', '2023-06-30', '2022-10-01', '2023-09-30', 'Q1', '2022-07-01', '2022-09-30', '2022-10-01', '2022-12-31'),
(2023, '2022-07-01', '2023-06-30', '2022-10-01', '2023-09-30', 'Q2', '2022-10-01', '2022-12-31', '2023-01-01', '2023-03-31'),
(2023, '2022-07-01', '2023-06-30', '2022-10-01', '2023-09-30', 'Q3', '2023-01-01', '2023-03-31', '2023-04-01', '2023-06-30'),
(2023, '2022-07-01', '2023-06-30', '2022-10-01', '2023-09-30', 'Q4', '2023-04-01', '2023-06-30', '2023-07-01', '2023-09-30'),
(2024, '2023-07-01', '2024-06-30', '2023-10-01', '2024-09-30', 'Q1', '2023-07-01', '2023-09-30', '2023-10-01', '2023-12-31'),
(2024, '2023-07-01', '2024-06-30', '2023-10-01', '2024-09-30', 'Q2', '2023-10-01', '2023-12-31', '2024-01-01', '2024-03-31'),
(2024, '2023-07-01', '2024-06-30', '2023-10-01', '2024-09-30', 'Q3', '2024-01-01', '2024-03-31', '2024-04-01', '2024-06-30'),
(2024, '2023-07-01', '2024-06-30', '2023-10-01', '2024-09-30', 'Q4', '2024-04-01', '2024-06-30', '2024-07-01', '2024-09-30');

Client Profile Table:

-- Create the Client table
  CREATE TABLE [dbo].[Client](
    [document_id] [int] NULL,
    [first_name] [varchar](max) NULL,
    [last_name] [varchar](max) NULL
  );
-- Insert values into the Client table
INSERT INTO Client VALUES
(10308040, 'Client1fn', 'Client1ln');

Program Enrollment Table:

-- Create the ProgramEnrollment table
  CREATE TABLE [dbo].[ProgramEnrollment](
    [RecordID] [int] NULL,
    [ServiceStartDate] [date] NULL,
    [ServiceEndDate] [date] NULL,
    [Program] [varchar](max) NULL,
  );
-- Insert values into the ProgramEnrollment table
INSERT INTO ProgramEnrollment
(130085, '2020-03-14', '2020-10-03', 'Program6');

Documents Table:

-- Create the Documents table
  CREATE TABLE [dbo].[Documents](
    [ParentID] [int] NULL,
    [RecordID] [int] NULL
  );
-- Insert values into the Documents table
INSERT INTO Documents
(10308040,130085);

What I have Tried: Join Statement Specifically:

JOIN Ref_Dates ref ON ((pe.ServiceStartDate <= ref.State_QEnd AND pe.ServiceEndDate >= ref.State_QStart) 
     OR (pe.ServiceStartDate <= ref.State_QEnd AND pe.ServiceStartDate >=ref.State_QStart 
         AND (pe.ServiceEndDate IS NULL OR pe.ServiceEndDate = '')))

Full Select Statement:

SELECT DISTINCT cp.document_id AS Client_ProfileID
,ref.Fiscal_Year
,ref.[Quarter]
,MIN(CAST(pe.ServiceStartDate AS DATE)) AS first_service
,CAST(pe.ServiceStartDate AS DATE) AS ServiceStartDate
,CASE
    WHEN pe.ServiceEndDate IS NULL THEN 
        CASE
            WHEN (MONTH(GETDATE()) > 10 OR (MONTH(GETDATE()) = 10 AND DAY(GETDATE()) >= 1)) AND pe.ServiceStartDate < CAST(CAST(YEAR(GETDATE()) AS VARCHAR) + '-10-01' AS DATE) THEN 
                CAST(CAST(YEAR(GETDATE()) AS VARCHAR) + '-10-01' AS DATE)
            ELSE 
                CAST(GETDATE() AS DATE)
        END
    ELSE 
        CAST(pe.ServiceEndDate AS DATE)
END AS ServiceEndDate

FROM Client_Profile AS cp
    JOIN documents docs ON docs.ParentId=cp.document_id
    JOIN ProgramEnrollment pe ON pe.RecordID=docs.RecordId
    JOIN Ref_Dates ref ON ((pe.ServiceStartDate <= ref.State_QEnd AND pe.ServiceEndDate >= ref.State_QStart) 
     OR (pe.ServiceStartDate <= ref.State_QEnd AND pe.ServiceStartDate >=ref.State_QStart 
         AND (pe.ServiceEndDate IS NULL OR pe.ServiceEndDate = '')))

GROUP BY cp.document_id
,ref.Fiscal_Year
,ref.[Quarter]
,pe.ServiceStartDate
,pe.ServiceEndDate

ORDER BY cp.document_id
GO
csga3l58

csga3l581#

By taking out the ref.[Quarter] from the group by and using MIN(ref.[Quarter]) AS [Quarter], it looks like you should get the results that I think you want.

SELECT DISTINCT cp.document_id AS Client_ProfileID
,ref.Fiscal_Year
,MIN(ref.[Quarter]) AS [Quarter]  --<<<< Here
,MIN(CAST(pe.ServiceStartDate AS DATE)) AS first_service
,CAST(pe.ServiceStartDate AS DATE) AS ServiceStartDate
,CASE
    WHEN pe.ServiceEndDate IS NULL THEN 
        CASE
            WHEN (MONTH(GETDATE()) > 10 OR (MONTH(GETDATE()) = 10 AND DAY(GETDATE()) >= 1)) AND pe.ServiceStartDate < CAST(CAST(YEAR(GETDATE()) AS VARCHAR) + '-10-01' AS DATE) THEN 
                CAST(CAST(YEAR(GETDATE()) AS VARCHAR) + '-10-01' AS DATE)
            ELSE 
                CAST(GETDATE() AS DATE)
        END
    ELSE 
        CAST(pe.ServiceEndDate AS DATE)
END AS ServiceEndDate

FROM #Client AS cp
    JOIN #documents docs ON docs.ParentId=cp.document_id
    JOIN #ProgramEnrollment pe ON pe.RecordID=docs.RecordId
    JOIN #Ref_Dates ref ON ((pe.ServiceStartDate <= ref.State_QEnd AND pe.ServiceEndDate >= ref.State_QStart) 
     OR (pe.ServiceStartDate <= ref.State_QEnd AND pe.ServiceStartDate >=ref.State_QStart 
         AND (pe.ServiceEndDate IS NULL OR pe.ServiceEndDate = '')))

GROUP BY cp.document_id
,ref.Fiscal_Year
--,ref.[Quarter]--<<<< Here
,pe.ServiceStartDate
,pe.ServiceEndDate

ORDER BY cp.document_id

相关问题