棘手的视图,仅显示大多数当前值以及任何尚未更新的旧值

时间:2019-12-13 22:05:23

标签: sql pivot max

我有一个表,该表可获取某些信息的更新,但不一定能定期获取所有信息。我需要一个查询,该查询返回所有最近的字段,但不返回任何重复项。例如,

Source Table View

在这里您可以看到我们在8/30的更新截止日期为12/31(以前是NULL)。我没有更新任何其他值,但是我仍然需要返回所有值以及新的关闭日期。

创建文件表的代码:

CREATE TABLE IntersectIOTest.[dbo].[Files_Dummy](
                [sysID] int,
                [ZipFileName] varchar (64),
                [TextFileName] varchar (64),
                [ImportBatchDate] date,
                [ImportFileDate] date,
                [ArchiveLocation] varchar (64),
                [ImportOutcome] varchar (16),
                [FileDescription] varchar (16),
                [ValuationDate] date,
                [FileSize] int
)

INSERT INTO IntersectIOTest.[dbo].[Files_Dummy]( 
            [sysID],
            [ZipFileName],
            [TextFileName],
            [ImportBatchDate],
            [ImportFileDate],
            [ArchiveLocation],
            [ImportOutcome],
            [FileDescription],
            [ValuationDate],
            [FileSize]
)

VALUES 
    (3040,'RT630D_40932_2019_05_25_0000017775.zip','RT630D_40932_2019_05_25_0000017775.txt','2019-05-25 05:00:23.000','2019-05-25 05:00:23.000','C:\File Storage\Archive','Successful','SpecialCoding','2019-05-25',NULL),
    (4783,'RT630D_40932_2019_08_31_0000017775.zip','RT630D_40932_2019_08_31_0000017775.txt','2019-08-31 02:00:29.000','2019-08-31 02:00:29.000','C:\File Storage\Archive','Successful','SpecialCoding','2019-08-31',NULL);

这是我当前拥有的代码:

WITH
FILES AS
(
SELECT      *
FROM        [dbo].[Files]
WHERE       ImportOutcome = 'Successful'
AND         FileDescription IN ('SpecialCoding')
),

SPCMAX AS
(
SELECT      *
FROM        (
            SELECT      MAX(FILES.ValuationDate) OVER (PARTITION BY [Claim-Number], Question /*addedd Question here*/) MAXVAL,
                        FILES.ValuationDate, FileID, [Claim-Number], [Question], [Response]
            FROM        [dbo].[SpecialCoding] SC
            INNER JOIN  FILES
            ON          SC.FileID = FILES.sysID
            WHERE       SC.Response IS NOT NULL -- Added this line here
            ) AS SC
PIVOT
            (
            MAX(Response)
            FOR Question IN ([Client Name], [Customer Number], [Record Only], [Reorting Source Indicator Description], [Estimated Claim Closure Date])
            ) AS PivotTable
WHERE       MAXVAL = ValuationDate
)

SELECT      ValuationDate, FileID, [Claim-Number], [Client Name], [Customer Number], [Record Only],
            [Reorting Source Indicator Description], [Estimated Claim Closure Date]
FROM        SPCMAX

我当前的脚本旋转数据并返回两行。

Current Results (Updated 12/26)

我需要的是

Needed view results

在此先感谢您的帮助。

12/18更新-这是我正在使用的真实表格 12/26更新-添加了用于创建“文件”表的代码和新的当前代码

Results for one claim where we are getting NULL values in the view we are creating

3 个答案:

答案 0 :(得分:0)

由于需求变化而更新2019-12-18

由于更新的信息,于2019-12-25更新

请以一种易于使用的方式发布您的数据,以便人们可以更轻松地为您提供帮助。不了解您的数据很难提供帮助。我继续为您完成,因此我们可以解决此问题。您还需要明确指定当前拥有的数据以及所需的确切方式。您多次更改了内容,引入了新的数据元素,并使用了一些不同的逻辑,这使其具有挑战性。

我能够使用CASE语句和PIVOT函数来获取解决方案。 PIVOT表可能需要清理一些,但是两种解决方案都能为您带来理想的结果。

用于创建表的SQL代码

CREATE TABLE [dbo].[SpecialCoding](
             [Record-Type] varchar(2),
             [Client-Program] int,
             [Event-Number] varchar(16),
             [Claim-Number] varchar(16),
             [Line-Of-Business] varchar(2),
             [Question-Number] int,
             [CP-Seq-Number] int,
             [Question] varchar(64),
             [Response] varchar(64),
             [Date-Added] date,
             [Date-Updated] date,
             [Filler] varchar(2),
             [FileID] int,
             [sysID] int
)

INSERT INTO [dbo].[SpecialCoding]( 
            [Record-Type],
            [Client-Program],
            [Event-Number],
            [Claim-Number],
            [Line-Of-Business],
            [Question-Number],
            [CP-Seq-Number],
            [Question],
            [Response],
            [Date-Added],
            [Date-Updated],
            [Filler],
            [FileID],
            [sysID]
)

VALUES 
       ('SC',29274,189008589,'189008589-001','WC',38,1,'Client Name','DALTON ENTERPRISES','20190524','20190524',NULL,3040,125045),
       ('SC',29274,189008589,'189008589-001','WC',2046,2,'Record Only','N','20190524','20190524',NULL,3040,125046),
       ('SC',29274,189008589,'189008589-001','WC',2558,3,'Customer Number',NULL,'20190524','20190524',NULL,3040,125047),
       ('SC',29274,189008589,'189008589-001','WC',2691,54,'Reporting Source Indicator Description','ELR','20190524','20190524',NULL,3040,125048),
       ('SC',29274,189008589,'189008589-001','WC',2842,55,'Estimated Claim Closure Date',NULL,'20190524','20190524',NULL,3040,125049),
       ('SC',29274,189008589,'189008589-001','WC',2842,55,'Estimated Claim Closure Date','2020-12-31','20190524','20190830',NULL,4783,138941);

带有案例解决方案的Windows功能

WITH CTE AS (
             SELECT SC.*
               FROM [dbo].[SpecialCoding] SC
                    INNER JOIN [dbo].[Files_Dummy] as FILES
                    ON SC.[FileID] = FILES.[sysID]
                       AND FILES.[ImportOutcome] = 'Successful'
                       AND FILES.[FileDescription] IN ('SpecialCoding')),

      MAXDATE AS( 
             SELECT [Claim-Number],
                    MAX([Date-Updated]) AS MAX_DATE
               FROM CTE
              GROUP BY [Claim-Number]),

     FILENUMBER AS(
               SELECT CTE.[Claim-Number],
                      CTE.[FileID]
                 FROM CTE
                      INNER JOIN MAXDATE MD
                      ON CTE.[Claim-Number] = MD.[Claim-Number]
                         AND CTE.[Date-Updated] = MD.[MAX_DATE]),
       SUB AS (
         SELECT *
           FROM (
                 SELECT CTE.*,
                        ROW_NUMBER() OVER(PARTITION BY [Claim-Number], [Question-Number] ORDER BY [Date-Updated] DESC) AS ROWN
                   FROM CTE) SUB  
                  WHERE SUB.[ROWN] = 1)

SELECT sub.*, 
       MAXDATE.MAX_DATE AS "Valuation Date",
       FILENUMBER.[FileID] 
  FROM (
        SELECT CTE.[Claim-Number],
               MAX(CASE WHEN CTE.[Question-Number] = 38 THEN CTE.Response ELSE NULL END) AS [Client Name],
               MAX(CASE WHEN CTE.[Question-Number] = 2046 THEN CTE.Response ELSE NULL END) AS [Record Only],
               MAX(CASE WHEN CTE.[Question-Number] = 2558 THEN CTE.Response ELSE NULL END) AS [Customer Number],
               MAX(CASE WHEN CTE.[Question-Number] = 2691 THEN CTE.Response ELSE NULL END) AS [Reporting Source],
               MAX(CASE WHEN CTE.[Question-Number] = 2842 THEN CTE.Response ELSE NULL END) AS [Estimated Closure Date]
          FROM CTE
         GROUP BY CTE.[Claim-Number]) sub
        LEFT JOIN MAXDATE
        ON MAXDATE.[Claim-Number] = sub.[Claim-Number]
        LEFT JOIN FILENUMBER
        ON FILENUMBER.[Claim-Number] = sub.[Claim-Number];

带有PIVOT解决方案的Windows功能

WITH CTE AS (
             SELECT SC.*
               FROM [dbo].[SpecialCoding] SC
                    INNER JOIN [dbo].[Files_Dummy] as FILES
                    ON SC.[FileID] = FILES.[sysID]
                       AND FILES.[ImportOutcome] = 'Successful'
                       AND FILES.[FileDescription] IN ('SpecialCoding')),

      MAXDATE AS( 
             SELECT [Claim-Number],
                    MAX([Date-Updated]) AS MAX_DATE
               FROM CTE
              GROUP BY [Claim-Number]),

     FILENUMBER AS(
               SELECT CTE.[Claim-Number],
                      CTE.[FileID]
                 FROM CTE
                      INNER JOIN MAXDATE MD
                      ON CTE.[Claim-Number] = MD.[Claim-Number]
                         AND CTE.[Date-Updated] = MD.[MAX_DATE]),
       SUB AS (
         SELECT *
           FROM (
                 SELECT CTE.*,
                        ROW_NUMBER() OVER(PARTITION BY [Claim-Number], [Question-Number] ORDER BY [Date-Updated] DESC) AS ROWN
                   FROM CTE) SUB  
                  WHERE SUB.[ROWN] = 1),

PT AS (
        SELECT *
          FROM (
                SELECT CTE.[Claim-Number],
                       CTE.[Question-Number],
                       CTE.[Response]
                  FROM CTE
                 ) sub
         PIVOT (
           MAX([Response])
           FOR [Question-Number] IN ([38],[2046],[2558],[2691],[2842])) AS pivot_table)
         SELECT PT.[38] AS "Client Name",
                PT.[2046] AS "Record Only",
                PT.[2558] AS "Customer Number",
                PT.[2691] AS "Reporting Source Indicator Description",
                PT.[2842] AS "Estimated Claim Closure Date", 
                MAXDATE.[MAX_DATE] AS [Valuation Date], 
                FILENUMBER.[FileID]
           FROM PT
                LEFT JOIN MAXDATE
                ON MAXDATE.[Claim-Number] = PT.[Claim-Number]
                LEFT JOIN FILENUMBER
                ON FILENUMBER.[Claim-Number] = PT.[Claim-Number]; 

案例解决方案

WITH FILES AS (
                SELECT *
                  FROM [dbo].[Files_Dummy]
                 WHERE ImportOutcome = 'Successful'
                  AND FileDescription IN ('SpecialCoding')),

     CTE AS (
             SELECT [Claim-Number],
                    [Question-Number],
                    MAX([Date-Updated]) AS MAX_DATE
               FROM [dbo].[SpecialCoding] SC
                    INNER JOIN FILES
                    ON SC.[FileID] = FILES.[sysID]
               GROUP BY [Claim-Number],[Question-Number]),

 MAXDATE AS( 
             SELECT [Claim-Number],
                    MAX([MAX_DATE]) AS MAX_DATE
               FROM CTE
              GROUP BY [Claim-Number]),

 FILENUMBER AS(
               SELECT SC.[Claim-Number],
                      SC.[FileID]
                 FROM [dbo].[SpecialCoding] SC
                      INNER JOIN MAXDATE MD
                      ON SC.[Claim-Number] = MD.[Claim-Number]
                         AND SC.[Date-Updated] = MD.[MAX_DATE]
                      INNER JOIN FILES
                      ON SC.[FileID] = FILES.[sysID])

SELECT sub.*, 
       MAXDATE.MAX_DATE AS "Valuation Date",
       FILENUMBER.[FileID] 
  FROM (
        SELECT t2.[Claim-Number],
               MAX(CASE WHEN t2.[Question-Number] = 38 THEN t2.Response ELSE NULL END) AS [Client Name],
               MAX(CASE WHEN t2.[Question-Number] = 2046 THEN t2.Response ELSE NULL END) AS [Record Only],
               MAX(CASE WHEN t2.[Question-Number] = 2558 THEN t2.Response ELSE NULL END) AS [Customer Number],
               MAX(CASE WHEN t2.[Question-Number] = 2691 THEN t2.Response ELSE NULL END) AS [Reporting Source],
               MAX(CASE WHEN t2.[Question-Number] = 2842 THEN t2.Response ELSE NULL END) AS [Estimated Closure Date]
          FROM [dbo].[SpecialCoding] t2
               INNER JOIN CTE 
               ON CTE.[Claim-Number] = t2.[Claim-Number]
                  AND CTE.[Question-Number] = t2.[Question-Number]
                  AND CTE.[MAX_DATE] = t2.[Date-Updated]
         GROUP BY t2.[Claim-Number]) sub
        LEFT JOIN MAXDATE
        ON MAXDATE.[Claim-Number] = sub.[Claim-Number]
        LEFT JOIN FILENUMBER
        ON FILENUMBER.[Claim-Number] = sub.[Claim-Number];

PIVOT解决方案

WITH FILES AS (
                SELECT *
                  FROM [dbo].[Files_Dummy]
                 WHERE ImportOutcome = 'Successful'
                  AND FileDescription IN ('SpecialCoding')),

     CTE AS (
             SELECT [Claim-Number],
                    [Question-Number],
                    MAX([Date-Updated]) AS MAX_DATE
               FROM [dbo].[SpecialCoding] SC
                    INNER JOIN FILES
                    ON SC.[FileID] = FILES.[sysID]
               GROUP BY [Claim-Number],[Question-Number]),

 MAXDATE AS( 
             SELECT [Claim-Number],
                    MAX([MAX_DATE]) AS MAX_DATE
               FROM CTE
              GROUP BY [Claim-Number]),

 FILENUMBER AS(
               SELECT SC.[Claim-Number],
                      SC.[FileID]
                 FROM [dbo].[SpecialCoding] SC
                      INNER JOIN MAXDATE MD
                      ON SC.[Claim-Number] = MD.[Claim-Number]
                         AND SC.[Date-Updated] = MD.[MAX_DATE]
                      INNER JOIN FILES
                      ON SC.[FileID] = FILES.[sysID]),

    PT AS (
    SELECT *
      FROM (
            SELECT t2.[Claim-Number],
                   t2.[Question-Number],
                   t2.[Response]
              FROM [dbo].[SpecialCoding] t2
                   INNER JOIN CTE 
                   ON CTE.[Claim-Number] = t2.[Claim-Number]
                      AND CTE.[Question-Number] = t2.[Question-Number]
                      AND CTE.[MAX_DATE] = t2.[Date-Updated]
             ) sub
     PIVOT (
       MAX([Response])
       FOR [Question-Number] IN ([38],[2046],[2558],[2691],[2842])) AS pivot_table)
     SELECT PT.[38] AS "Client Name",
            PT.[2046] AS "Record Only",
            PT.[2558] AS "Customer Number",
            PT.[2691] AS "Reporting Source Indicator Description",
            PT.[2842] AS "Estimated Claim Closure Date", 
            MAXDATE.[MAX_DATE] AS [Valuation Date], 
            FILENUMBER.[FileID]
       FROM PT
            LEFT JOIN MAXDATE
            ON MAXDATE.[Claim-Number] = PT.[Claim-Number]
            LEFT JOIN FILENUMBER
            ON FILENUMBER.[Claim-Number] = PT.[Claim-Number]; 

证明解决方案有效

enter image description here

让我引导您完成我尝试做的事情。

您不确定FILES公用表表达式(CTE),因为我看不到这些数据。

CTE的CTE试图获取此信息。

enter image description here

对于Claim-NumberQuestion的每种组合,只能拉出具有最大日期的组合。这意味着红色行将不会显示在结果中。我之所以将Question-Number留在那儿,只是因为它与Question是一对一的关系,并且不会影响合并。

然后,将您的完整数据集和内部联接带入我们在字段CTEClaim-NumberQuestion和{{1}中创建的Question-Number }。彩色行匹配,因此白色背景中的行不会显示在结果中。这意味着Date-UpdatedClaim-NumberQuestionQuestion-Number在两个数据集上都完全匹配的地方,请保留这些行。

enter image description here enter image description here

所以现在我们有了以下数据集。 enter image description here

现在,使用Date-Updated语句来透视数据并获取下面的数据集。

enter image description here

现在在顶部,我们创建了CASE,它是下面的数据集。红色所有行将不会显示在结果中,因为我们要做的只是为该MAXDATE选择MAX Date-Updated

enter image description here

使用Claim-Number连接数据透视图以获得最终结果。

enter image description here

然后根据您对MAXDATE的逻辑,也可以将其引入。好像您要拉File-ID来获取最新的File-ID,而其中的Response是最新的。希望可以帮助清除其中的一部分!老实说,我会稍微更改查询并使用Date-Updated而不是Question-Number。这也具有挑战性,因为需求不断变化,并且您多次引入了新数据。

答案 1 :(得分:0)

在阅读了枢纽功能的参考页之后,我意识到我已经省略了用于枢纽的基本数据(“文件”表)。因此,为了从清晰,完整的信息入手,以下是我要修复的源代码:

WITH FILES AS(
              SELECT *
                FROM [dbo].[Files]
               WHERE ImportOutcome = 'Successful'
                 AND FileDescription IN ('SpecialCoding')
              ),
       CTE AS(
              SELECT [Claim-Number],
                     Question,
                     MAX([Date-Updated]) AS MAX_DATE
                FROM [dbo].[SpecialCoding] SC
                     INNER JOIN FILES
                     ON FILES.sysID = SC.FileID
               GROUP BY [Claim-Number], Question),

   MAXDATE AS( SELECT [Claim-Number],
                      MAX(MAX_DATE) AS MAX_DATE
                 FROM CTE
                GROUP BY [Claim-Number])

SELECT sub.*
FROM (
        SELECT t2.[Claim-Number],
               CASE WHEN t2.Question = 'Client Name' THEN t2.Response ELSE NULL END AS [Client Name],
               CASE WHEN t2.Question = 'Record Only' THEN t2.Response ELSE NULL END AS [Record Only],
               CASE WHEN t2.Question = 'Customer Number' THEN t2.Response ELSE NULL END AS [Customer Number],
               CASE WHEN t2.Question = 'Reporting Source Indicator Description' THEN t2.Response ELSE NULL END AS [Reporting Source],
               CASE WHEN t2.Question = 'Estimated Claim Closure Date' THEN t2.Response ELSE NULL END AS [Estimated Closure Date]
          FROM [dbo].[SpecialCoding] t2
               INNER JOIN CTE 
               ON CTE.[Claim-Number] = t2.[Claim-Number]
                  AND CTE.Question = t2.Question
                  AND CTE.MAX_DATE = t2.[Date-Updated]) sub
        LEFT JOIN MAXDATE
        ON MAXDATE.[Claim-Number] = sub.[Claim-Number]

最终使它工作了,但结果不正确。我期待这样的事情(忽略前几列,因为我们没有在查询中放入它们):

Expected Results Table (for one claim)

但是,相反,我的索赔编号显示为多行:

Actual results with code above

12/27:将记录添加到SpecialCoding表

INSERT INTO [dbo].[SpecialCoding]( 
            [Record-Type],
            [Client-Program],
            [Event-Number],
            [Claim-Number],
            [Line-Of-Business],
            [Question-Number],
            [CP-Seq-Number],
            [Question],
            [Response],
            [Date-Added],
            [Date-Updated],
            [Filler],
            [FileID],
            [sysID]
)

VALUES 
      ('SC',029274,186106950,'186106950-001','WC',38,1,'Client Name','JARP INDUSTRIES','2012-03-01','2012-03-01,NULL,89,841),
('SC',029274,186106950,'186106950-001','WC',2046,2,'Record Only',NULL,'2012-03-01','2012-03-01',NULL,89,842),
('SC',029274,186106950,'186106950-001','WC',2558,3,'Customer Number',17542,'2011-09-17','2012-03-01',NULL,89,843),
('SC',029274,186106950,'186106950-001','WC',2691,54,'Reporting Source Indicator Description',NULL,'2012-03-01','2012-03-01',NULL,89,844),
('SC',029274,186106950,'186106950-001','WC',38,1,'Client Name','JARP INDUSTRIES','2012-03-01','2012-03-01',NULL,90,49677),
('SC',029274,186106950,'186106950-001','WC',2046,2,'Record Only',NULL,'2012-03-01','2012-03-01',NULL',90,49678),
('SC',029274,186106950,'186106950-001','WC',2558,3,'Customer Number',17542, '2011-09-17','2012-03-01',NULL,90,49679),
('SC',029274,186106950,'186106950-001','WC',2691,54,'Reporting Source Indicator Description',NULL,'2012-03-01','2012-03-01',NULL,90,49680);

答案 2 :(得分:0)

这有效!

WITH
FILES AS
(
SELECT      *
FROM        [dbo].[Files]
WHERE       ImportOutcome = 'Successful'
AND         FileDescription IN ('SpecialCoding')
),

SPCMAX AS -- added max values at top of subquery here or all values except the [claim-number]
(
SELECT      MAX(ValuationDate) AS ValuationDate, MAX(FileID) AS FileID, [Claim-Number], MAX([Client Name]) AS [Client Name],
            MAX([Customer Number]) AS [Customer Number], MAX([Record Only]) AS [Record Only],
            MAX([Reorting Source Indicator Description]) AS [Reorting Source Indicator Description],
            MAX([Estimated Claim Closure Date]) AS [Estimated Claim Closure Date]
FROM        (
            SELECT      MAX(FILES.ValuationDate) OVER (PARTITION BY [Claim-Number], Question /*addedd Question here*/) MAXVAL,
                        FILES.ValuationDate, FileID, [Claim-Number], [Question], [Response]
            FROM        [dbo].[SpecialCoding] SC
            INNER JOIN  FILES
            ON          SC.FileID = FILES.sysID
            WHERE       SC.Response IS NOT NULL -- Added this line here
            ) AS SC
PIVOT
            (
            MAX(Response)
            FOR Question IN ([Client Name], [Customer Number], [Record Only], [Reorting Source Indicator Description], [Estimated Claim Closure Date])
            ) AS PivotTable
WHERE       MAXVAL = ValuationDate
GROUP BY    [Claim-Number] -- added this
)

SELECT      ValuationDate, FileID, [Claim-Number], [Client Name], [Customer Number], [Record Only],
            [Reorting Source Indicator Description], [Estimated Claim Closure Date]
FROM        SPCMAX