Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

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
0
phil301
Asked:
phil301
  • 4
  • 3
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
phil301Author Commented:
Currently, if there is zero parts in the shelf location, it just does not display the shelf location.
0
 
reb73Commented:
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
 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now