Solved

SQL Grouping with Zero Value

Posted on 2011-03-01
8
281 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
  • 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
 

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now