SQL query no results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbedford
    New Member
    • May 2010
    • 23

    #1

    SQL query no results

    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, qryPrintCostAss ets, returns all data from tblAssets based on a selection in a form. qryPrintCostAss ets.ID is key to the rest of the queries pulling their data.

    Code:
    SELECT *
    FROM tblAssets
    WHERE (((tblAssets.ID)=Forms!formPrintCost!listPrinter));
    qryPrintCostSup port pulls from tblSupport based on qryPrintCostAss ets.ID. When there are no entries in tblSupport for a particular printer, qryPrintCostSup port returns a row of empty cells.

    Code:
    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;
    qryPrintCostMet er does the same as qryPrintCostSup port, 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 qryPrintCostSup port. No entries in tblPrinterMeter results in a row of empty cells.

    Then there are 2 queries dependent on these two queries. qryPrintCostSup portCostTotals sums qryPrintCostSup port.Cost:

    Code:
    SELECT qryPrintCostAssets.ID AS AssetID, Sum(qryPrintCostSupport.Cost) AS SCost
    FROM qryPrintCostAssets INNER JOIN qryPrintCostSupport ON qryPrintCostAssets.ID=qryPrintCostsupport.AssetID
    GROUP BY qryPrintCostAssets.ID;
    If the results of qryPrintCostSup port are a row of empty cells, the result of qryPrintCostSup portCostTotals is no cells under the column titles.

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

    When either qryPrintCostMet erTotals or qryPrintCostSup portCostTotals has no cells, qryPrintCostMas ter also returns no cells under the column headers/titles.

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

    Code:
    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;
    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 qryPrintCostMas ter to return the expected row of values simply excluding those for which there are none?
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    You might want to try using an outer join, rather than an inner join.

    Just because you see a blank row in the output display doesn't mean that there is actually anything there.

    Comment

    • mbedford
      New Member
      • May 2010
      • 23

      #3
      Is the syntax the same for INNER and OUTER joins?

      What are the substantive differences between the two?

      What are examples of times one and the other are appropriate?

      Comment

      • mbedford
        New Member
        • May 2010
        • 23

        #4
        Ok, figured this out with the pointers you gave, Oralloy. Thank you.

        I want to be able to do more things with the two queries qryPrintCostSup port and qryPrintCostMet er, so the fix had to work by that point in the query ladder.

        So I changed the INNER JOINs in those two queries to RIGHT JOINs (which Access trimmed down from RIGHT OUTER JOINs that I'd typed). When there is no data in either tblSupport or tblPrinterMeter for a selected printer, they return a single record with only the AssetID populated.

        This gets handed down to qryPrintCostMet erTotals and qryPrintCostSup portCostTotals which, because it's an actual record and not a blank return, show the same record with only the AssetID populated. And the qryPrintCostMas ter shows all available information for the selected printer as a single record without erroring when data is not available in one or both of those fields.

        Thank you for the help.

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Glad to help.

          Good luck with your project.

          Oralloy

          Comment

          Working...