简体   繁体   中英

Join multiple rows give one distinct row

My SQL statement returns the following

SQLID  Col1  Col2  Col3
14945  NULL  NULL  sdf
14945  NULL  xyz   NULL
14945  abc   NULL  NULL

where as I would like it to return

SQLID   Col1 Col2   Col3
14945   abc  xyz    sdf

Please help.

The schema is such that I have a table which contains SQLID 's. And a second table which contains SQLID 's as the FK to first table and the col1, col2 and col3. Currently I am just doing a join on SQLId

Select 
    t1.SQLID, t2.Col1, t2.col2, t2.col3
from
    Table1 t1 
join
    table2 t2 on t1.SQLId = t2.SQLid

EDIT: if Col1, Col2, Col3 are DATETIME datatype (as per following comment) You can use;

SELECT SQLID, MAX(ISNULL(Col1,0)) AS Col1,
        MAX(ISNULL(Col2,0)) AS Col2, MAX(ISNULL(Col3,0)) AS Col3
FROM YourTable
GROUP By SQLID

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