SQL Case statement to determine to add or subtract quantity

I have a situation where the table I'm using stores all quantities as a positive  values.  A "Sales" record would have document type = 1 with a positive quantity value and a return would have a document type = 2 with a positive quantity value.

I'd like to use a sql statement in my stored proc to add the quantities for all "Sales" records within a date range, then subtract all quantities for "Returns" records within that same date range to establish a net sales quantity.

I'm thinking of a case statement, but I suppose a subquery could work also.

I've you done similar, I would like to see how you accomplished this.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
A subselect would work fine. I've used made-up field names, so simply replace with your field names, and with your table name:

SELECT  SalesPerson,
        Type1Qty - Type2Qty AS NetQty
FROM   (SELECT  SalesPerson,
                    WHEN DocumentType = 1 THEN Quantity
                    ELSE 0
                END) AS Type1Qty,
                    WHEN DocumentType = 2 THEN Quantity
                    ELSE 0
                END) AS Type2Qty
        FROM    YourSalesTable
        GROUP BY SalesPerson) t

Open in new window

Also, in the subselect, if you need to add more non-summing fields, you'll need to add them to the GROUP BY as well.
Shaun KlineLead Software EngineerCommented:
You can use your document type to determine if you should multiply your quantities by either 1 or -1.
case when documenttype = 2 then -1 else 1 end * quantity

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
    SUM(sales_amount * CASE WHEN document_type = 2 THEN -1 ELSE 1 END) AS Net_Sales_Amount
FROM table_name
    sales_date >= @start_date AND
    sales_date < @end_date
JBM2015Author Commented:
Thank You, I'll give those a try.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.