Solved

SQL query returns blank cells or no cells

Posted on 2010-08-31
1
951 Views
Last Modified: 2012-05-10
How can I make a query return zero values when there are no results?

The first level of queries returns data from tables based on a selection made in a form and reported in a different query.

The base query, qryPrintCostAssets, returns all data from tblAssets based on a selection in a form. qryPrintCostAssets.ID is key to the rest of the queries pulling their data.

SELECT *
FROM tblAssets
WHERE (((tblAssets.ID)=Forms!formPrintCost!listPrinter));

Open in new window


qryPrintCostSupport pulls from tblSupport based on qryPrintCostAssets.ID. When there are no entries in tblSupport for a particular printer, qryPrintCostSupport returns a row of empty cells.

SELECT qryPrintCostAssets.ID AS AssetID, Left([tblSupport.Problem],50) AS Problem, tblSupport.Date, tblSupport.DownTime, tblSupport.Cost
FROM tblSupport INNER JOIN qryPrintCostAssets ON tblSupport.AssetID=qryPrintCostAssets.ID
ORDER BY tblSupport.ID;

Open in new window


qryPrintCostMeter does the same as qryPrintCostSupport, except it is pulling from tblPrinterMeter for meter readings (page counts). The syntax is essentially the same except the fields are numeric except for the AssetsID field, which is exactly the same as in qryPrintCostSupport. No entries in tblPrinterMeter results in a row of empty cells.

Then there are 2 queries dependent on these two queries. qryPrintCostSupportCostTotals sums qryPrintCostSupport.Cost:

SELECT qryPrintCostAssets.ID AS AssetID, Sum(qryPrintCostSupport.Cost) AS SCost
FROM qryPrintCostAssets INNER JOIN qryPrintCostSupport ON qryPrintCostAssets.ID=qryPrintCostsupport.AssetID
GROUP BY qryPrintCostAssets.ID;

Open in new window


If the results of qryPrintCostSupport are a row of empty cells, the result of qryPrintCostSupportCostTotals is no cells under the column titles.

The same holds true for qryPrintCostMeterTotals, which uses the MAX value from the qryPrintCostMeter fields to find the highest value for BCount (black), CCount (color), and TCount (total). If qryPrintCostMeter is a row of empty cells, qryPrintCostMeterTotals returns no cells under the column headers.

When either qryPrintCostMeterTotals or qryPrintCostSupportCostTotals has no cells, qryPrintCostMaster also returns no cells under the column headers/titles.

qryPrintCostMaster uses multiple INNER JOINs to pull data from several other queries all tracing back to qryPrintCostAssets in one way or another for the purpose of populating fields in a form to track printer costs.

SELECT qryPrintCostAssets.ID AS AssetID, qryPrintCostAssets.UID, qryPrintCostAssets.ModelID, qryPrintCostAssets.Status, qryPrintCostAssets.Dept, qryPrintCostAssets.SubDept, qryPrintCostAssets.SN, qryPrintCostAssets.Box, qryPrintCostAssets.BoxDrop, qryPrintCostAssets.NetName, qryPrintCostAssets.MACAddress, qryPrintCostAssets.IPAddress, qryPrintCostAssets.Description, qryPrintCostAcquire.ID AS AcquireID, qryPrintCostAcquire.Method, qryPrintCostAcquire.Price, qryPrintCostAcquire.DateAcquired, qryPrintCostAcquire.Number, qryPrintCostAcquire.LeaseBegin, qryPrintCostAcquire.LeaseEnd, qryPrintCostAcquire.Vendor, qryPrintCostMeterTotals.BCount, qryPrintCostMeterTotals.CCount, qryPrintCostMeterTotals.TCount, qryPrintCostSupportCostTotals.SCost
FROM ((qryPrintCostAssets INNER JOIN qryPrintCostAcquire ON qryPrintCostAssets.ID=qryPrintCostAcquire.AssetID) INNER JOIN qryPrintCostSupportCostTotals ON qryPrintCostAssets.ID=qryPrintCostSupportCostTotals.AssetID) INNER JOIN qryPrintCostMeterTotals ON qryPrintCostAssets.ID=qryPrintCostMeterTotals.AssetID;

Open in new window


What is the difference between a query returning empty/blank cells and a query returning no cells?

Is there error correction or handling that I can employ in SQL that would ultimately allow qryPrintCostMaster to return the expected row of values simply excluding those for which there are none?
0
Comment
Question by:music2myear1
[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
1 Comment
 
LVL 6

Accepted Solution

by:
music2myear1 earned 0 total points
ID: 33570870
Ok. Resolved this with help from another site and more investigation of OUTER JOINs.

RIGHT OUTER JOINs in place of the INNER JOINs in qryPrintCostSupport and qryPrintCostMeter allowed the query to return a single record with only the AssetID populated in cases where the associated tables had no records that matched.

These successful queries then were queried by qryPrintCostSupportCostTotals and qryPrintCostMeterTotals which returned the same mostly empty record. And qryPrintCostMaster then showed only the available data if some data was not populated, instead of failing altogether.

It was important to me to fix this by qryPrintCostMeter and qryPrintCostSupport because I want to use the data from these queries to work in other parts of the Print Cost form.
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

688 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