Solved

SQL query returns blank cells or no cells

Posted on 2010-08-31
1
909 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
1 Comment
 
LVL 6

Accepted Solution

by:
music2myear1 earned 0 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

12 Experts available now in Live!

Get 1:1 Help Now