SQL - TSQL summing of values

I am new to learning SQL and am trying to figure out how to so some summing on a field within a query.

I have a table that holds the Quantity On Hand (QOH) for each inventory item in all 4 stores. If I run a query for one specific inventory item the results look like this:

Store            InventoryID   QOH
1      196196      0
2      196196      2
3      196196      -1
4      196196      0

I need to be able to sum the QOH and be able to use that number in the where clause. Here's the catch though...If the value of the QOH is <= 0 then I want the value set to 0. I don't want any negative numbers. In the above data if I do a sum the total comes out to be 1 rather than 2 (which I know 1 is mathmatically correct). So my question is how do I parse though the QOH on each store and set the negatives = 0 and then sum the amount for the QOH for each InventoryID.

Any suggestions on how to do this would be great.

Thanks.
LVL 1
ochnessAsked:
Who is Participating?

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

x
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.

sdstuberCommented:
select sum(case when qoh < 0 then 0 else qoh end) from yourtable
0
sdstuberCommented:
including inventoryid

select inventoryid, sum(case when qoh < 0 then 0 else qoh end) from yourtable
group by inventoryid
0

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
LowfatspreadCommented:
to use the "sum" in a where clause will require you to use a subquery...

or can you use the having clause...?
what are you trying to do..?

you may get better performance by putting the qoh condition into the where clause in the first place...


select inventoryid,sum(qoh) as qoh
from yourtable
where qoh>0
group by inventoryid
having sum(qoh) > 5 -- if you're looking for a "certain" overstocking level...
order by invetoryid


or
select inventoryid,qoh
from (
select inventoryid,sum(qoh) as qoh
from yourtable
where qoh>0
group by inventoryid
) as x
where qoh ....
order by inventoryid
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
sdstuberCommented:
http:#a37280233 does exactly what was asked.

The desired answer is 2 as noted by the asker's comment

" In the above data if I do a sum the total comes out to be 1 rather than 2 "

 the post above returns the desired result

easy enough to demonstrate

WITH yourtable
     AS (SELECT 1 store, 196196 inventoryid, 0 qoh
         UNION ALL
         SELECT 2, 196196, 2
         UNION ALL
         SELECT 3, 196196, -1
         UNION ALL
         SELECT 4, 196196, 0)
SELECT   inventoryid, SUM(CASE WHEN qoh < 0 THEN 0 ELSE qoh END)
    FROM yourtable
GROUP BY inventoryid
0
sdstuberCommented:
other than a small typo

http:#a37284146  also returns the correct answer as seen here (I removed the erroneous order by)

WITH yourtable
     AS (SELECT 1 store, 196196 inventoryid, 0 qoh
         UNION ALL
         SELECT 2, 196196, 2
         UNION ALL
         SELECT 3, 196196, -1
         UNION ALL
         SELECT 4, 196196, 0)
select inventoryid,sum(qoh) as qoh
from yourtable
where qoh>0
group by inventoryid

or the other query

WITH yourtable
     AS (SELECT 1 store, 196196 inventoryid, 0 qoh
         UNION ALL
         SELECT 2, 196196, 2
         UNION ALL
         SELECT 3, 196196, -1
         UNION ALL
         SELECT 4, 196196, 0)
select inventoryid,qoh
from (
select inventoryid,sum(qoh) as qoh
from yourtable
where qoh>0
group by inventoryid
) as x


the first post also returns the correct result but assumes inventoryid has been prefiltered
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sorry for that, sdstuber. You are correct, totally missed that comment.

New recommendation:
  Accept:  http:#a37280233

Qlemo
Cleanup Volunteer
0
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 2008

From novice to tech pro — start learning today.