Reputation: 241
I've been trying to look for a way to sum a specific set of values in a query.
Currently I have a query that returns all values needed, but I want it to now sum several values.
|Name|Value|
|x |1 |
|x |2 |
|x |3 |
|x |5 |
|y |3 |
|y |2 |
|y |2 |
|y |3 |
|z |3 |
|z |2 |
|z |1 |
I don't know if I should run a subquery, I'm not necessarily summing up distinct values, but instead have something along the lines of this:
|Name|Value|
|x |11 |
|y |10 |
|z |6 |
Although, each entry has their own unique ID for their respective row. I'm fairly new at this so I don't know if I would take that into account with my query.
Upvotes: 0
Views: 92
Reputation: 4154
A CTE might be the easiest way to go here:
;WITH CTE AS (Your Query Here)
SELECT Name, SUM(Value) AS Value
FROM CTE
GROUP BY Name
Upvotes: 0
Reputation: 1804
Select q.name, sum(q.value)
from (YOUR_SELECT_QUERY) q
group by q.name
Upvotes: 1