Reputation: 9146
I have a table with some data similar to this:
col1 | col2
-----------
val1 1
val1 5
val1 7
val2 3
val2 1
val2 4
val3 8
What I would like is a query to produce something like this:
col1 | sum
----------
val1 13
val2 8
val3 8
I've tried doing this with complicated subqueries, but nothing is quite panning out. I think I may be overthinking it.
Upvotes: 1
Views: 123
Reputation: 247840
You just need to use the aggregate function sum()
with a group by
:
select col1, SUM(col2) total
from yourtable
group by col1
Upvotes: 3
Reputation: 34063
This should be all you need. This uses the aggregate SUM
with a GROUP BY
.
SELECT col1, SUM(col2)
FROM unicorns
GROUP BY col1
Upvotes: 3