[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL query returns blank cells or no cells

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this article, we’ll look at how to deploy ProxySQL.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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