ginsonic
asked on
SQL .. SUM .. SUBSQL ?
I have next table structure:
Name product ... buy/sale ... quantity
Cats .... 1 ( I buy ) ... 10
Dogs ... 1 ... 20
Birds ... 1 ... 14
Cats .... -1 ( I sell ) ... 5
Dogs ... -1... 7
Cats ... 1 ... 12
I need a sql that will return me:
Cats ... 17
Dogs ... 13
Birds ... 14
Can help me with a SQL script.
Name product ... buy/sale ... quantity
Cats .... 1 ( I buy ) ... 10
Dogs ... 1 ... 20
Birds ... 1 ... 14
Cats .... -1 ( I sell ) ... 5
Dogs ... -1... 7
Cats ... 1 ... 12
I need a sql that will return me:
Cats ... 17
Dogs ... 13
Birds ... 14
Can help me with a SQL script.
ASKER
Sorry, was so easier:
Select Name,Sum(saleorbuy*quantit y) from Table group by Name
Select Name,Sum(saleorbuy*quantit
ASKER
I will ask to delete this topic
well wasn't my answer helpfull? if not, then maybe a paq with refund should be better, no?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
you can try something like this (not tested):
select product,sum(prod) from (select product,action*quantity as prod from table ) group by product
let me know if there are any errors as I don't have anything to test it against