?
Solved

SQL Grouping with Zero Value

Posted on 2011-03-01
8
Medium Priority
?
324 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 2000 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

741 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