0

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.

3
  • 1
    why can't you just do a select sum(Value) ... group by Name? Commented Feb 29, 2016 at 16:47
  • There is a lot more to my select statement, there are more than two columns, a join, a date range, and an order by. I wanted to see how the sum works and build upon that in my query. Commented Feb 29, 2016 at 16:52
  • 2
    then you should put that into your question. we can't read your mind, after all... Commented Feb 29, 2016 at 17:00

2 Answers 2

1
Select q.name, sum(q.value) 
from (YOUR_SELECT_QUERY) q
group by q.name
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.