将多行合并为一行的SQL
我希望将多行合并为一行,并且只保留值不为NULL的值 以下是我想要实现的目标: 我想从这个将多行合并为一行的SQL,sql,sql-server,group-by,Sql,Sql Server,Group By,我希望将多行合并为一行,并且只保留值不为NULL的值 以下是我想要实现的目标: 我想从这个 +----+-----------------+-----------------+-----------------+--------------------+ | ID | 1stNofification | 2ndNotification | 3rdNotification | NotificationNumber | +----+-----------------+----------------
+----+-----------------+-----------------+-----------------+--------------------+
| ID | 1stNofification | 2ndNotification | 3rdNotification | NotificationNumber |
+----+-----------------+-----------------+-----------------+--------------------+
| 1 | 01.01.2019 | NULL | NULL | 1 |
+----+-----------------+-----------------+-----------------+--------------------+
| 1 | NULL | 02.02.2019 | NULL | 2 |
+----+-----------------+-----------------+-----------------+--------------------+
| 1 | NULL | NULL | 03.03.2019 | 3 |
+----+-----------------+-----------------+-----------------+--------------------+
| 2 | 06.01.2019 | NULL | NULL | 1 |
+----+-----------------+-----------------+-----------------+--------------------+
| 2 | NULL | 09.02.2019 | NULL | 2 |
+----+-----------------+-----------------+-----------------+--------------------+
| 2 | NULL | NULL | 11.03.2019 | 3 |
+----+-----------------+-----------------+-----------------+--------------------+
为此:
+----+-----------------+-----------------+-----------------+
| ID | 1stNofification | 2ndNotification | 3rdNotification |
+----+-----------------+-----------------+-----------------+
| 1 | 01.01.2019 | 02.02.2019 | 03.03.2019 |
+----+-----------------+-----------------+-----------------+
| 2 | 06.01.2019 | 09.02.2019 | 11.03.2019 |
+----+-----------------+-----------------+-----------------+
我试过这样的方法:
选择
身份证件
最大值(当a.NotificationNumber=1时,则1通知结束)1通知,
最大值(当a.NotificationNumber=2时,则为第2个通知结束)第2个通知,
最大值(当a.NotificationNumber=3时,则为3通知结束)3通知
来自通知
按ID分组
但不幸的是,这并没有给我预期的结果
如果有人能帮助我,我将不胜感激:)您只需在没有任何案例的情况下使用max即可
SELECT
ID,
MAX(1stNotification) AS 1stNotification,
MAX(2ndNotification) AS 2ndNotification,
MAX(3rdNotification) AS 3rdNotification
FROM Notifications
GROUP BY ID
你只需要在没有任何案例的情况下使用max
SELECT
ID,
MAX(1stNotification) AS 1stNotification,
MAX(2ndNotification) AS 2ndNotification,
MAX(3rdNotification) AS 3rdNotification
FROM Notifications
GROUP BY ID
我想你需要这样的东西
; with cte as (
select 1 as id, 'dd' as not1, null as not2, null as not3 , 1 as notifications
union all
select 1, null, 'df', null , 2
union all
select 1, null, null, 'vc', 3
union all
select 2, 'ws', null, null, 1
union all
select 2, null, 'xs', null, 2
union all
select 2, null, null, 'nm', 3
)
, ct as (
select id, coalesce(not1, not2, not3) as ol, notifications ,
'notification' + cast(notifications as varchar(5)) as Col
from cte
)
select * from (
select id, ol, col from ct )
as d
pivot (
max(ol) for col in ( [notification1], [notification2], [notification3] )
) as P
据我所知,这里的结果中的通知栏实际上是在行中提及的通知编号 我想你需要这样的东西
; with cte as (
select 1 as id, 'dd' as not1, null as not2, null as not3 , 1 as notifications
union all
select 1, null, 'df', null , 2
union all
select 1, null, null, 'vc', 3
union all
select 2, 'ws', null, null, 1
union all
select 2, null, 'xs', null, 2
union all
select 2, null, null, 'nm', 3
)
, ct as (
select id, coalesce(not1, not2, not3) as ol, notifications ,
'notification' + cast(notifications as varchar(5)) as Col
from cte
)
select * from (
select id, ol, col from ct )
as d
pivot (
max(ol) for col in ( [notification1], [notification2], [notification3] )
) as P
据我所知,这里的结果中的通知栏实际上是在行中提及的通知编号 因此,可以确定每个
Id
只有一个NotificationNumber
@DarkRob是的,每个Id只有一个NotificationNumber,因此可以确定每个Id
只有一个NotificationNumber
@DarkRob是的,每个Id只有一个NotificationNumber这对我不起作用,它仍然给我包含空值的列,而不是它应该包含的日期pick@calgara12 . . . 这应该行得通。如果没有,那么你的数据与你的问题不同。@GordonLinoff你是对的,我犯了一个错误,现在它起作用了。谢谢这对我不起作用,它仍然给我包含空值的列,而不是应该包含的日期pick@calgara12 . . . 这应该行得通。如果没有,那么你的数据与你的问题不同。@GordonLinoff你是对的,我犯了一个错误,现在它起作用了。谢谢