Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining rows in SQL Server

Tags:

sql-server

I'm trying to combine rows in SQL Server.

Assume I have a table like:

  C1 |  C2  | C3
  1  |  A   | 
  1  |      | 
  1  |      |  B
  2  |  A   |  
  2  |      |  C

And I want to end up with:

  C1 |  C2  | C3
  1  |  A   |  B
  2  |  A   |  C

Any way I can do this with one query?

At the moment I'm parsing the data manually with c#, but it's slow, and I can't limit the number of rows that are returned easily.

Thanks in advance!

like image 673
JTirr Avatar asked Dec 21 '22 03:12

JTirr


1 Answers

For your example data

SELECT C1,
       MAX(C2) AS C2,
       MAX(C3) AS C3
FROM   YourTable
GROUP  BY C1 

SQL Fiddle

like image 89
Martin Smith Avatar answered Jan 10 '23 03:01

Martin Smith