简体   繁体   中英

How I Can Merge Mutiple Rows into One in SQL Server

ID   col1   col2   col3 
------------------------
1     A     null   null 
1    null    B     null 
1    null   null    C 
2     D     null   null 
2    null    E     null 
2    null   null    F

From this table, remove null value and merge rows base on ID

ID   col1   col2  col3 
------------------------
1     A      B     C 
2     D      E     F 

You can use aggregation for that, as aggregate functions ignore NULL values:

select id, 
       max(col1) as col1, 
       max(col2) as col2,
       max(col3) as col3
from the_table
group by id
order by id;

Online example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM