【问题标题】:Separating rows into columns将行分成列
【发布时间】:2021-05-02 21:28:56
【问题描述】:

状态更改的日期被收集在一列中,无论状态是发送还是重新发送等。我希望将两个日期分成两列。如果有人可以提供帮助,那就太好了!

我再次使用 SSMS 2018。

我的桌子是什么样子的:

+-------+--------+--------+------------+
| Code  | Name   | Status | StatusDate |
+-------+--------+--------+------------+     
|   221 | Kim    |  Resent|  2020-01-05|         
|   221 | Kim    |   Sent |  2020-01-01|             
+-------+--------+--------+---------+--+  

我想要的样子:

+-------+--------+--------+-----------+-----------+
| Code  | Name   | Type   | SentDate  | ResentDate| 
+-------+--------+--------+-----------+-----------+     
|   221 | Kim    |   Book | 2020-01-01| 2020-01-05|                   
+-------+--------+--------+---------+-------------+  

【问题讨论】:

  • 请向我们展示您的尝试以及您遇到的问题。你为什么要标记 CSS?那与查询无关?

标签: sql sql-server tsql rows


【解决方案1】:

我会使用条件聚合:

select code, name, type,
       max(case when status = 'Sent' then statusdate end) as sent_date,
       max(case when status = 'Resent' then statusdate end) as resent_date
from t
group by code, name, type;

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 2016-03-26
    • 2017-01-20
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 2015-09-08
    • 2021-10-29
    • 2020-05-09
    相关资源
    最近更新 更多