Sql 将行分隔为列
状态更改的日期收集在一列中,无论状态是发送还是重新发送,等等。我希望将这两个日期分为两列。如果有人能帮忙,那就太好了 我再次使用SSMS 2018 我的桌子是什么样子的:Sql 将行分隔为列,sql,sql-server,tsql,rows,Sql,Sql Server,Tsql,Rows,状态更改的日期收集在一列中,无论状态是发送还是重新发送,等等。我希望将这两个日期分为两列。如果有人能帮忙,那就太好了 我再次使用SSMS 2018 我的桌子是什么样子的: +-------+--------+--------+------------+ | Code | Name | Status | StatusDate | +-------+--------+--------+------------+ | 221 | Kim | Resent| 2020-01
+-------+--------+--------+------------+
| 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|
+-------+--------+--------+---------+-------------+
我会使用条件聚合:
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;
请告诉我们您尝试了什么,以及您遇到了什么困难。为什么要给CSS添加标签?这与查询无关?