codeFinatic
codeFinatic

Reputation: 171

Getting Sum of one row SQL Server

I am currently getting executing a select statement. I just want to get a sum of one of the rows and not the other 3. I have the following:

SELECT SUM([Amount]), [Number], [Brand]
FROM [myTable]
WHERE [ID] = '1000'

I just want to get the sum of Amount. If I take out Number and Brand and only select Amount it works great, but not with them included... any ideas?

SELECT SUM([Amount]), [Number], [Brand]
FROM [myTable]
WHERE [ID] = '1000' 

Upvotes: 0

Views: 3875

Answers (2)

Caleb Palmquist
Caleb Palmquist

Reputation: 458

You are probably getting an error because the other selections are not being used in an aggregate function.

To resolve you may want to try grouping, something like this:

SELECT   SUM(Amount) AS Amount, Number, Brand
FROM     [yourtable]
WHERE    ID = 1000
GROUP BY Number, Brand

Upvotes: 1

Duniyadnd
Duniyadnd

Reputation: 4043

You need to group it.

SELECT 
    SUM([Amount]), [Number], [Brand]
FROM 
    [myTable]
WHERE 
    [ID] = '1000'
GROUP BY
    Number , Brand

Upvotes: 5

Related Questions