2016-08-03 21 views
0

我的表上有以下記錄。我想根據主密鑰將這些記錄轉換成一行。根據主密鑰將多個行轉換爲一個行

records:- 
id  date  userkey workinghours masterkey 
1941 2016-06-01 1   6   NULL 
1946 2016-06-08 1   1   NULL 
1947 2016-06-09 1   6   1946 
1948 2016-06-10 1   3   1946 

我想要這樣的輸出。

id  startdate endate  userkey workinghours masterkey 
1941 2016-06-01 2016-06-01 1  6   NULL 
1946 2016-06-08 2016-06-10 1  10   1946 

我該怎麼做?作爲新手,我需要您的幫助來撰寫此查詢。

+5

憑什麼請問'workinghours'輸出得到值6,10 – jonju

+1

和1946年id如何在輸出中獲得masterkey 1946,而在樣本數據中,masterkey是否爲null? –

+0

當記錄有組密鑰我想總結工作時間@jonju –

回答

2

這裏是一個辦法做到這一點,利用ISNULLGROUP BY

創建和填充示例表(保存我們這一步在你未來的問題)

DECLARE @T as TABLE 
(
    id int, 
    [date] date, 
    userkey int, 
    workinghours int, 
    masterkey int 
) 

INSERT INTO @T VALUES 
(1941, '2016-06-01', 1, 6, NULL), 
(1946, '2016-06-08', 1, 1, NULL), 
(1947, '2016-06-09', 1, 6, 1946), 
(1948, '2016-06-10', 1, 3, 1946) 

查詢:

SELECT ISNULL(MasterKey, id) as id, 
     MIN([date]) as startdate, 
     MAX([date]) as enddate, 
     userkey, 
     SUM(workinghours) as workinghours, 
     MIN(masterKey) as masterKey 
FROM @T 
GROUP BY ISNULL(MasterKey, id), userkey 

結果:

id   startdate enddate userkey  workinghours masterKey 
----------- ---------- ---------- ----------- ------------ ----------- 
1941  2016-06-01 2016-06-01 1   6   NULL 
1946  2016-06-08 2016-06-10 1   10   1946 
+0

更好的和更優雅的方式與'ISNULL'都已經在一個選擇 –

+0

@GiladGreen謝謝! –

+0

這是不正確的,因爲第一個記錄的結束日期應該是'2016-06-01' – Ket

0

試試這個

DECLARE @T as TABLE 
(
    id int, 
    [date] date, 
    userkey int, 
    workinghours int, 
    masterkey int 
) 

INSERT INTO @T VALUES 
(1941, '2016-06-01', 1, 6, NULL), 
(1946, '2016-06-08', 1, 1, NULL), 
(1947, '2016-06-09', 1, 6, 1946), 
(1948, '2016-06-10', 1, 3, 1946) 


SELECT 
    MIN(ISNULL(masterkey,id)) as Id, 
    MIN([date]) as StartDate, 
    MAX([date]) as EndDate, 
    min(userkey) As Userkey, 
    sum(workinghours) As WorkingHours, 
    MIN(MasterKey) AS MasterKey 
FROM 
(
    SELECT 
     id , 
     date , 
     userkey , 
     workinghours , 
     masterkey, 
     ISNULL(masterkey, id) TmpMasterkey 
    FROM @T  
) A 
GROUP BY TmpMasterkey 

輸出

Id  StartDate EndDate  Userkey WorkingHours MasterKey 
1941 2016-06-01 2016-06-01 1  6    NULL 
1946 2016-06-08 2016-06-10 1  10    1946 
0

這應該工作:

create table #tbl 
(id  INT 
, dt date   
, userkey INT 
, workinghours INT 
, masterkey INT 
) 

INSERT INTO #tbl VALUES 
(1941 ,'2016-06-01' , 1   ,6   ,NULL) 
,(1946 ,'2016-06-08' ,1   ,1   ,NULL) 
,(1947 ,'2016-06-09' ,1   ,6   ,1946) 
,(1948 ,'2016-06-10' ,1   ,3   ,1946) 

WITH WORKHOURS_CTE (
    ID 
    ,origdate 
    ,userkey 
    ,workinghours 
    ,masterkey 
    ) 
AS 
    -- Define the CTE query. 
    (
    SELECT CASE WHEN masterkey IS NULL THEN id ELSE masterkey END AS ID 
     ,dt 
     ,userkey 
     ,workinghours 
     ,masterkey 
    FROM #tbl 
    ) 
-- Define the outer query referencing the CTE name. 
SELECT ID 
    ,MIN(origdate) AS STARTDATE 
    ,MAX(origdate) AS ENDDATE 
    ,SUM(workinghours) AS workinghours 
    ,userkey 
    ,MAX(masterkey) AS masterkey 
FROM WORKHOURS_CTE 
GROUP BY ID 
    ,userkey;