?
Solved

SQL query returns blank cells or no cells

Posted on 2010-08-31
1
Medium Priority
?
954 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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