根据主密钥将多行转换为一行

时间:2016-08-03 06:54:23

标签: sql sql-server

我的桌子上有以下记录。我想根据主密钥将这些记录转换为一行。

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

我该怎么做?作为一个新手,我需要你的帮助来编写这个查询。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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;