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?
 
sdstuberConnect With a Mentor Commented:
including inventoryid

select inventoryid, sum(case when qoh < 0 then 0 else qoh end) from yourtable
group by inventoryid
0
 
sdstuberCommented:
select sum(case when qoh < 0 then 0 else qoh end) from yourtable
0
 
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
QlemoBatchelor, 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
 
QlemoBatchelor, 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.