Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning the sum of items depending on which type it is

Tags:

sql

sql-server

I have one field that I need to sum lets say named items However that field can be part of group a or b In the end I need to have all of the items summed for group a and group b

when I say grouped I mean there is a LEFT OUTER JOIN to another table the previous table has a type for the items and the one being joined has a group assigned for this item type

Sorry guys Im a little new to sql I am going to try out what you have given me an get back to you

Ok I feel like we are getting close just not yet allain's I can get them to separate but the issue I need to have both groups to sum on the same row which is difficult because I also have several LEFT OUTER JOIN's involved

Tyler's looks like it might work too so I am trying to hash that out real fast

Alain's seems to be the way to go but I have to tweek it a little more

like image 793
Josh Mein Avatar asked Dec 15 '25 21:12

Josh Mein


1 Answers

Maybe I'm not understanding the complexity of what you're asking but... shouldn't this do?

SELECT groupname, SUM(value)
FROM items
WHERE groupname IN ('a', 'b')
GROUP BY groupname

And if you don't care which of a or b the item belongs to then this will do:

SELECT SUM(value)
FROM items
WHERE groupname IN ('a', 'b')
like image 92
Allain Lalonde Avatar answered Dec 17 '25 20:12

Allain Lalonde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!