4

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!

1
  • Can your data rows have C2 and C3 populated in the same row? Can a C1 value have multiple rows where when you combine the rows both C2 and C3 would have multiple values? Commented Jul 12, 2013 at 20:16

1 Answer 1

11

For your example data

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

SQL Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It didn't occur to me to use max on strings. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.