skyofwin
skyofwin

Reputation: 17

How can multiple rows into a single line?

I currently have a query from one table which returns multiple rows. I would like is to combine these into one row for each person.

Is there a way to do this?

Example data from query and result I need:

http://upic.me/i/wf/stack.jpg

Upvotes: 1

Views: 249

Answers (2)

Guffa
Guffa

Reputation: 700182

Use grouping and aggregates:

select
  max(Seq) as Seq,
  ID,
  sum(RED1) as RED1,
  sum(RED2) as RED2,
  sum(RED3) as RED3,
  sum(GREEN1) as GREEN1,
  sum(GREEN2) as GREEN2,
  sum(GREEN3) as GREEN3
from
  Table 1
group by
  ID

Demo: http://sqlfiddle.com/#!4/ad36e/3

Note: You would use different aggregates for the values depending on what result you want if a field has more than one non-null value in a group. Using sum gives you the sum of the values in the group, using max gives you the highest value in the group.

Upvotes: 4

Sagar Joon
Sagar Joon

Reputation: 1417

Try this query:

select ID , max(seq) Seq, max(red1) RED1 , max(red2) RED2, max(red3) RED3, 
max(green1) GREEN1, max(green2) GREEN2, max(green3) GREEN3
from Table1
group by ID;

Upvotes: 3

Related Questions