phadaphunk
phadaphunk

Reputation: 13313

Regroup common rows

I have a query that will give me 4 duplicates everytime because one field is different.

Let's say I have this table

-----------------
| A |  B | C | D |
-----------------
| 1 |Blue| 2 | 0 |
| 1 |Blue| 2 | 1 |
| 1 |Blue| 2 | 2 |
| 1 |Blue| 2 | 3 |
| 2 |Red | 1 | 0 |
| 2 |Red | 1 | 1 |
| 2 |Red | 1 | 2 |
| 2 |Red | 1 | 3 |
------------------

What I would like to do is regroup them into one. As for the D column they should be grouped into one using SUM().

The thing is I don't know where to start. Is there a keyword to group them into one ? I would have used Distinct but I will still have 4 of them because of 1 mismatch.

The final reuslt set should be

------------------
| A |  B | C | D |
------------------
| 1 |Blue| 2 | 6 |
| 2 |Red | 1 | 6 |
------------------

Is it even possible ?

Upvotes: 0

Views: 56

Answers (1)

mr.Reband
mr.Reband

Reputation: 2430

sure it's possible -- this should do it:

 select A, B, C, sum(D) as D
 from TABLE
 group by A, B, C

Upvotes: 1

Related Questions