将SQL行值转换为列

时间:2019-03-28 09:01:17

标签: sql sql-server

我从SQL表中得到一个结果,需要将一列的值拆分为另外两列。

列名“ attributename”将始终具有两个值“ CompletedDate”和“ CompletedExitDate”。在列名“ endvalue”中具有相应的时间戳记值。

我们需要将“ CompletedDate”和“ CompletedExitDate”显示为两个不同的新列,并从“ endvalue”中显示相应的时间戳记值。

实际表格输出:

transactionid   objectid       attributeid transactiondate         username         attributename       endvalue
42120           8291            1062        03/25/19  2:28:05 PM    JOHN            CompletedDate       3/25/2019 2:28:06 PM
41911           8291            1096        03/22/19  3:18:59 PM    WF_SERVICE      CompletedExitDate   3/22/2019 3:18:59 PM
41910           8291            1062        03/22/19  3:10:58 PM    JOHN            CompletedDate       3/22/2019 3:10:59 PM
41669           8291            1096        03/21/19  2:57:10 PM    WF_SERVICE      CompletedExitDate   3/21/2019 2:57:10 PM
41661           8291            1062        03/21/19  2:12:51 PM    JOHN            CompletedDate       3/21/2019 2:12:52 PM

该表的预期输出:

transactionid   objectid    attributeid transactiondate   username  CompletedDate       CompletedExitDate
42120           8291        1062        3/25/19 2:28 PM   JOHN       3/25/19 2:28 PM    Null
41910           8291        1062        3/22/19 3:10 PM   JOHN       3/22/19 3:10 PM    3/22/19 3:18 PM
41661           8291        1062        3/21/19 2:12 PM   JOHN       3/21/19 2:12 PM    3/21/19 2:57 PM

尝试查询1:

SELECT
    A.[objectid], 
    max(case when attributename = 'CompletedDate'  then A.[endvalue] end) AS CompletedDate,
    max(case when attributename = 'CompletedExitDate'  then A.[endvalue] end) AS CompletedExitDate
FROM
    RMOBJECTHISTORY A join rmattribute B on A.attributeid= B.attributeid 
    where (A.[objectid]=8291 and  (B.attributename='CompletedDate' and A.[endvalue] <> '')) or (A.[objectid]=8291 and (B.attributename='CompletedExitDate' and A.[endvalue] <> '')) 
GROUP BY
   A.[objectid],transactiondate
ORDER BY
    transactiondate desc ;

已尝试查询1的输出:

objectid    CompletedDate       CompletedExitDate
8291        3/25/19 2:28 PM     NULL
8291        NULL                3/22/19 3:18 PM
8291        3/22/19 3:10 PM     NULL
8291        NULL                3/21/19 2:57 PM
8291        3/21/19 2:12 PM     NULL

尝试查询2:

SELECT
    A.[objectid], transactionid,attributeid,
    max(case when attributename = 'CompletedDate'  then A.[endvalue] end) AS CompletedDate,
    max(case when attributename = 'CompletedExitDate'  then A.[endvalue] end) AS CompletedExitDate FROM
    RMOBJECTHISTORY A join rmattribute B on A.attributeid= B.attributeid 
    where (A.[objectid]=8291 and  (B.attributename='CompletedDate' and A.[endvalue] <> '')) or (A.[objectid]=8291 and (B.attributename='CompletedExitDate' and A.[endvalue] <> ''))  GROUP BY    A.[objectid],transactionid,attributeid

查询2的输出:

objectid    transactionid   attributeid CompletedDate       CompletedExitDate
8291        41661           1062        3/21/19 2:12 PM     NULL
8291        41910           1062        3/22/19 3:10 PM     NULL
8291        42120           1062        3/25/19 2:28 PM     NULL
8291        41669           1096        NULL                3/21/19 2:57 PM
8291        41911           1096        NULL                3/21/19 2:57 PM

问题是,对于每个“ CompletedDate”,都有一个对应的“ CompletedExitDate”,该名称列在初始查询的“ endvalue”中。当我们尝试转换为新列时,它将转到值为“ NULL”的新行。

我们最终需要下面的输出:

CompletedDate      CompletedExitDate
3/21/19 2:12 PM    3/21/19 2:57 PM
3/22/19 3:10 PM    3/21/19 2:57 PM
3/25/19 2:28 PM    NULL

2 个答案:

答案 0 :(得分:0)

从分组依据中删除transactiondate

SELECT
    A.[objectid], 
    max(case when attributename = 'CompletedDate'  then A.[endvalue] end) AS CompletedDate,
    max(case when attributename = 'CompletedExitDate'  then A.[endvalue] end) AS CompletedExitDate
FROM
    RMOBJECTHISTORY A join rmattribute B on A.attributeid= B.attributeid 
    where (A.[objectid]=8291 and  (B.attributename='CompletedDate' and A.[endvalue] <> '')) or (A.[objectid]=8291 and (B.attributename='CompletedExitDate' and A.[endvalue] <> '')) 
GROUP BY
   A.[objectid]

答案 1 :(得分:0)

您必须transactionid,attributeid进行选择并进行分组

SELECT
    A.[objectid], transactionid,A.attributeid,
    max(case when attributename = 'CompletedDate'  then A.[endvalue] end) AS CompletedDate,
    max(case when attributename = 'CompletedExitDate'  then A.[endvalue] end) AS CompletedExitDate
FROM
    RMOBJECTHISTORY A join rmattribute B on A.attributeid= B.attributeid 
    where (A.[objectid]=8291 and  (B.attributename='CompletedDate' and A.[endvalue] <> '')) or (A.[objectid]=8291 and (B.attributename='CompletedExitDate' and A.[endvalue] <> '')) 
GROUP BY
   A.[objectid],transactionid,A.attributeid