2

同様の質問:時間のリスト内のシーケンスアイランドに基づいて行をクロス集計する

次のデータ例を見てください。

Employ     Date          Duration
0000001    2012-11-12    9.00
0000001    2012-11-13    9.00
0000001    2012-11-14    9.00
0000001    2012-11-16    9.00
0000001    2012-11-17    9.00
0000002    2012-11-04    9.00
0000002    2012-11-05    9.00
0000002    2012-11-06    9.00
0000003    2012-11-01    9.00
0000004    2012-11-02    6.50

私はこれを次のように変換しようとしています:

Employ     Start Date    End Date      Days    TotalDuration
0000001    2012-11-12    2012-11-14    3       27
0000001    2012-11-16    2012-11-17    2       18
0000002    2012-11-04    2012-11-06    3       27
0000003    2012-11-01    2012-11-01    1       9.00
0000004    2012-11-02    2012-11-02    1       6.50

上記のリンクされた質問を使用して、私はこれを次のようにやってみました:

WITH    ExampleData ( [DET_NUMBERA], [Date], [Duration] )
          AS ( 
                SELECT '0000001' AS [DET_NUMBERA], CONVERT(DATE, '12/11/2012', 103) AS [Date], 9.00 AS [Duration]
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '13/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '14/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '16/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '17/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000002', CONVERT(DATE, '04/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000002', CONVERT(DATE, '05/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000002', CONVERT(DATE, '06/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000003', CONVERT(DATE, '01/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000004', CONVERT(DATE, '02/11/2012', 103), 6.5
             ),
        CrossTabPrep
          AS ( SELECT   [DET_NUMBERA] ,
                        [Date] ,
                        [Duration] ,
                        CONVERT(INT, CONVERT(VARCHAR(10), GETDATE(), 112)) - ROW_NUMBER() OVER ( PARTITION BY [DET_NUMBERA] ORDER BY [Date] ) AS [Grp]
               FROM     ExampleData
             ),
        FinalRender ( [DET_NUMBERA], [Start Date], [End Date], [Days], [Duration] )
          AS ( SELECT   [DET_NUMBERA] ,
                        MIN([Date]) AS [Start Date] ,
                        MAX([Date]) AS [End Date] ,
                        ISNULL(DATEDIFF(DAY, MIN([Date]), MAX([Date])), 0) AS [Days] ,
                        SUM([Duration]) AS [Duration]
               FROM     CrossTabPrep
               GROUP BY [DET_NUMBERA] ,
                        [Grp]
             )
    SELECT  *
    FROM    FinalRender
    ORDER BY [DET_NUMBERA]

しかし、それは完全には機能していません-どこかでうまくいかなかった-上記のようにこのSQLを微調整して変換するにはどうすればよいですか?

4

2 に答える 2

0

問題の一部は、FinalRenderあなたがフィールドにいることGROUP BYにあります。これはあなたの合計を捨て[Grp]ている値を作っています:DISTINCT

WITH    ExampleData ( [DET_NUMBERA], [Date], [Duration] )
  AS ( 
        SELECT '0000001' AS [DET_NUMBERA], CONVERT(DATE, '12/11/2012', 103) AS [Date], 9.00 AS [Duration]
        UNION ALL
        SELECT '0000001', CONVERT(DATE, '13/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000001', CONVERT(DATE, '14/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000001', CONVERT(DATE, '15/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000001', CONVERT(DATE, '16/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000002', CONVERT(DATE, '04/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000002', CONVERT(DATE, '05/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000002', CONVERT(DATE, '06/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000003', CONVERT(DATE, '01/11/2012', 103), 9.00
        UNION ALL
        SELECT '0000004', CONVERT(DATE, '02/11/2012', 103), 6.5
     ),
CrossTabPrep
  AS ( SELECT   [DET_NUMBERA] ,
                [Date] ,
                [Duration] ,
                CONVERT(INT, CONVERT(VARCHAR(10), GETDATE(), 112)) - ROW_NUMBER() OVER ( PARTITION BY [DET_NUMBERA] ORDER BY [Date] ) AS [Grp]
       FROM     ExampleData
     ),
FinalRender ( [DET_NUMBERA], [Start Date], [End Date], [Days], [Duration] )
  AS ( SELECT   [DET_NUMBERA] ,
                MIN([Date]) AS [Start Date] ,
                MAX([Date]) AS [End Date] ,
                ISNULL(DATEDIFF(DAY, MIN([Date]), MAX([Date])), 0) AS [Days] ,
                SUM([Duration]) AS [Duration]
       FROM     CrossTabPrep
       GROUP BY [DET_NUMBERA] 
     )
SELECT  *
FROM    FinalRender
ORDER BY [DET_NUMBERA]
于 2012-11-19T15:23:37.717 に答える
0

ドー!getDate()[日付]列の代わりにGrp識別子を使用していました。

次のように改訂-正常に動作し、休憩を処理します:

WITH    ExampleData ( [DET_NUMBERA], [Date], [Duration] )
          AS ( 
                SELECT '0000001' AS [DET_NUMBERA], CONVERT(DATE, '12/11/2012', 103) AS [Date], 9.00 AS [Duration]
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '13/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '14/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '16/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000001', CONVERT(DATE, '17/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000002', CONVERT(DATE, '04/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000002', CONVERT(DATE, '05/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000002', CONVERT(DATE, '06/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000003', CONVERT(DATE, '01/11/2012', 103), 9.00
                UNION ALL
                SELECT '0000004', CONVERT(DATE, '02/11/2012', 103), 6.5
             ),
        CrossTabPrep
          AS ( SELECT   [DET_NUMBERA] ,
                        [Date] ,
                        [Duration] ,
                        CONVERT(INT, CONVERT(VARCHAR(10), [Date], 112)) - ROW_NUMBER() OVER ( PARTITION BY [DET_NUMBERA] ORDER BY [Date] ) AS [Grp]
               FROM     ExampleData
             ),
        FinalRender ( [DET_NUMBERA], [Start Date], [End Date], [Days], [Duration] )
          AS ( SELECT   [DET_NUMBERA] ,
                        MIN([Date]) AS [Start Date] ,
                        MAX([Date]) AS [End Date] ,
                        ISNULL(DATEDIFF(DAY, MIN([Date]), MAX([Date])), 0) + 1 AS [Days] ,
                        SUM([Duration]) AS [Duration]
               FROM     CrossTabPrep
               GROUP BY [DET_NUMBERA] ,
                        [Grp]
             )
    SELECT  *
    FROM    FinalRender
    ORDER BY [DET_NUMBERA]
于 2012-11-19T15:51:53.237 に答える