Solved

SQL Grouping with Zero Value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

910 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

25 Experts available now in Live!

Get 1:1 Help Now