Link to home
Create AccountLog in
Avatar of ginsonic
ginsonicFlag for Romania

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.
Avatar of 2266180
2266180
Flag of United States of America image

should have posted in some DB TA as this has not much to do with delphi ;)

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
Avatar of ginsonic

ASKER

Sorry, was so easier:

Select Name,Sum(saleorbuy*quantity) from Table group by Name
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
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer