同様の質問:時間のリスト内のシーケンスアイランドに基づいて行をクロス集計する
次のデータ例を見てください。
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を微調整して変換するにはどうすればよいですか?