【问题标题】:Conversion of SQL Row values into Column将 SQL 行值转换为列
【发布时间】:2019-03-28 17:44:28
【问题描述】:

我有一个来自 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

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    从群组中删除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]
    

    【讨论】:

      【解决方案2】:

      你必须 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
      

      【讨论】:

      • 您好 Zaynul,感谢您的回复。我们尝试了您更新的查询,但我们得到的是空值。请参考更新后的问题。
      • 在您的问题中分享您的原始表格值
      • 请参考实际表格输出和预期表格输出
      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 2011-10-04
      • 1970-01-01
      相关资源
      最近更新 更多