question

Saarmedh avatar image
Saarmedh asked

How do I combine data from two rows with a duplicated column value

Table A

Col1      Col2     Col3             Col4     Col5
1         AA       12/12/2018       1        null
2         AA1      12/5/2017        2        null
3         AA       15/7/2019        null     5

I need result to be like so:

Table A

Col1         Col2       Col3         Col4       Col5
1            AA         12/12/2018   1          5
2            AA1        12/5/2017    2          null

I need to combine results from rows for Col4 and Col5 where col2 is duplicated, and take the smallest date in Col3.

Not getting an idea for this one.

t-sqlselectgroup-by
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
GPO avatar image
GPO answered

@Saarmedh This should help.

SELECT tba.Col2
    ,min(tba.Col3) as Col3
    ,max(tba.Col4) as Col4
    ,max(tba.Col5) as Col5
FROM table_a tba
GROUP BY tba.Col2
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.