SQL Grouping with Zero Value

I am attempting to sum up the number of parts on hand for given groups in the OrgTble column. The query below is working correctly, but it is not showing groups that have zero items in stock.

I would like for it to show something similar to the table below (It would not currently show the Shelf C 0).

Shelf A   5
Shelf B 10
Shelf C 0

How can I get the query below to show groups that do not currently have items in stock?

SELECT OrgTable, SUM(QtyOnHand) AS Quantity FROM All_Assets WHERE
(PartNumber = '015533913') GROUP BY OrgTable
phil301Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
reb73Connect With a Mentor Commented:
Change the query as follows -

SELECT OrgTable, SUM(CASE WHEN PartNumber = '015533913' THEN QtyOnHand ELSE 0 END) AS Quantity
FROM All_Assets
GROUP BY OrgTable

But a warning, this will return 0 for all OrgTable values regardeless of whether they stock the relevant partnumber or not.
0
 
Rajkumar GsSoftware EngineerCommented:
Are you sure 'Shelf C' is in 'All_Assets ' table for PartNumber = '015533913' ?
0
 
phil301Author Commented:
Yes, this is a table with shelf locations of parts. If I enter a part number, I want it to return me all of the shelf locations and the number of parts in that location.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Rajkumar GsSoftware EngineerCommented:
When we use GROUP BY OrgTable , the query should show distinct OrgTable data, if present in that table.
If the first column 'OrgTable' has some master table, then you can use JOIN and make this work
0
 
phil301Author Commented:
Currently, if there is zero parts in the shelf location, it just does not display the shelf location.
0
 
phil301Author Commented:
All of the data is present in the All_Assets table
0
 
Rajkumar GsSoftware EngineerCommented:
What is the master table for 'Shelf A', 'Shelf B', ... data - I mean the column 'OrgTable' refers to which master table ?
0
 
phil301Author Commented:
Thanks reb, thats got it. It does pose another problem with selecting everything though.
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.