简体   繁体   中英

SQL query to group a column and ignore null values

I have a table like:

Col1 Col2 Col3 Col4
1          a
1     b          
1                c
2     e   
2          f  
2                g

I need to write a query which will have the output like this

Col1 Col2 Col3 Col4
1     a     b   c
2     e     f   g

I am using oracle 10g

If you only have one value per column, then you might be able to use an aggregate function:

select 
  col1,
  max(col2) col2,
  max(col3) col3,
  max(col4) col4
from yourtable
group by col1

See SQL Fiddle with Demo

The result is:

| COL1 | COL2 | COL3 | COL4 |
-----------------------------
|    1 |    b |    a |    c |
|    2 |    e |    f |    g |

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