Gali
Gali

Reputation: 14953

Group by query and SQL Server 2008

I have this query

select SUM(Qty) as Qty
from WorkTbl  group by Status
having Status = 'AA' or Status = 'BB'

this query returns 2 rows (100 and 500)

How to sum those 2 rows ?

Upvotes: 0

Views: 178

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239646

Take out the GROUP BY, and use WHERE instead of HAVING?

select SUM(Qty) as Qty
from WorkTbl
where Status = 'AA' or Status = 'BB'

Or, if there's more to your query, and you wish to keep most of the current structure, put it into a subquery (or a CTE):

select SUM(Qty) from (
select SUM(Qty) as Qty
from WorkTbl  group by Status
having Status = 'AA' or Status = 'BB'
) t

(We have to include the t at the end, since every row source in the from clause has to have a name - it could be anything)

Upvotes: 3

Related Questions