Midhun
Midhun

Reputation: 47

sum of row values depending on the id

I'm trying to get the sum of adult and child in new table based on cid from the same table.

table sales

+-----------+------------+---------------+
| cid       | adult      |  child        |
+-----------+------------+---------------+
|         1 |     20     |      20       | 
|         1 |     30     |      10       |
|         1 |    100     |      35       |
|         1 |     10     |      25       |
|         2 |     40     |      10       |
|         2 |     20     |      70       |
|         2 |     30     |      60       | 
+-----------+------------+---------------+

desired output:

+-----------+------------+---------------+
| cid       | adult      |  child        |
+-----------+------------+---------------+
|         1 |     160    |       90      | 
|         2 |      90    |      140      |
+-----------+------------+---------------+

plz help me

Upvotes: 0

Views: 38

Answers (2)

Sean Johnson
Sean Johnson

Reputation: 5607

SELECT cid,sum(adult) adult, sum(child) child FROM sales GROUP BY cid

Upvotes: 1

Hanky Panky
Hanky Panky

Reputation: 46900

SELECT cid, SUM(adult) AS adult, SUM(child) AS child 
FROM sales 
GROUP BY cid

Upvotes: 3

Related Questions