Solved

SQL Grouping with Zero Value

Posted on 2011-03-01
8
311 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:phil301
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35008152
Are you sure 'Shelf C' is in 'All_Assets ' table for PartNumber = '015533913' ?
0
 

Author Comment

by:phil301
ID: 35008174
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
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35008180
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:phil301
ID: 35008190
Currently, if there is zero parts in the shelf location, it just does not display the shelf location.
0
 
LVL 25

Accepted Solution

by:
reb73 earned 500 total points
ID: 35008213
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
 

Author Comment

by:phil301
ID: 35008215
All of the data is present in the All_Assets table
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35008219
What is the master table for 'Shelf A', 'Shelf B', ... data - I mean the column 'OrgTable' refers to which master table ?
0
 

Author Closing Comment

by:phil301
ID: 35008273
Thanks reb, thats got it. It does pose another problem with selecting everything though.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question