Reputation: 1483
I know how to do a SUM
in MySQL but how add a column like this (ungroup the group ?)
CREATE TABLE Table1
(`id` int, `price` int)
;
INSERT INTO Table1
(`id`, `price`)
VALUES
(1,10),
(1, 10),
(2, 20),
(2, 20),
(3, 30),
(3, 31)
;
ID would be think like a 'category ID'. I want this :
ID price sum
--------------------
1 10 20 <- or nothing if it's not to hard
1 10 20
2 20 40
2 20 40
3 30 61
3 31 61
Not this :
ID price sum
--------------------
1 10 20
2 20 40
3 30 61
sql :
SELECT id, SUM(price)FROM Table1 GROUP BY id
http://sqlfiddle.com/#!2/34fbe/1
Upvotes: 0
Views: 2433
Reputation: 1269503
You can use a subquery:
select t.*, tt.sumprice
from table1 t join
(select id, sum(price) as sumprice
from table1 t
group by id
) tt
on t.id = tt.id;
Upvotes: 2