Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

Micorosft Access SQL - Counting Number of foreign key records across 3 related tables

I am experienced at VBA but new to SQL.

I am developing a test sheet program in MS Access for the plant that I work at. This test sheet program will be used across 3 product lines.

When an order is created, it can contain up to all 3 products. The products are unique enough that I cannot put them all into their own table. So I have broken the test sheets up into 3 tables, each table representing its respective product test sheet. Please see the image below for my relationship setup.

Relationship Table

What I am trying to do:

I am trying to design a query that will be my master list (outputting to a continuous form). The master list will show all orders, and also show how many units have been tested in each order. See below for my desired output.

My Issue: The query is outputting the incorrect counts

It is not properly counting the number of related records. See the linked photo.

I know my key field is Order Number but I am searching by Job name. Originally my key field was job name but then switched it to order number.

thank you for your time, I am happy to provide more information if needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Consider joining three aggregate saved queries:

SELECT OrderNumber
     , COUNT(*) AS CountofCassetteTests 
FROM TblCassetteTestSheet
GROUP BY OrderNumber
SELECT OrderNumber
     , COUNT(*) AS CountofSentinelTests 
FROM TblSentinelTestSheet
GROUP BY OrderNumber
SELECT OrderNumber
     , COUNT(*) AS CountofHUVTests
FROM TblHUVTestSheet
GROUP BY OrderNumber

Then join to TblJobName in your final query (parentheses are required):

SELECT j.OrderNumber, j.JobName
     , c.CountofCassetteTests AS [# Of Cassettes Tested]
     , s.CountofSentinelTests AS [# Of Sentinels Tested]
     , h.CountofHUVTests AS [# Of HUV Tested]
     , j.JobEndDate, j.SPONumber
FROM ((TblJobName j
LEFT JOIN QryCassetteTestSheet c
    ON j.OrderNumber = c.OrderNumber)
LEFT JOIN QrySentinelTestSheet s
    ON j.OrderNumber = s.OrderNumber)
LEFT JOIN QryHUVTestSheet h
    ON j.OrderNumber = h.OrderNumber

Conceivably you can run all in one query using subqueries (and maybe one day even Common Table Expressions, CTEs, if the MS Access team ever enhances its older SQL dialect):

SELECT j.OrderNumber, j.JobName
     , c.CountofCassetteTests AS [# Of Cassettes Tested]
     , s.CountofSentinelTests AS [# Of Sentinels Tested]
     , h.CountofHUVTests AS [# Of HUV Tested]
     , j.JobEndDate, j.SPONumber
FROM ((TblJobName j
LEFT JOIN 
    (SELECT OrderNumber, COUNT(*) AS CountofCassetteTests 
     FROM TblCassetteTestSheet
     GROUP BY OrderNumber) c
  ON j.OrderNumber = c.OrderNumber)
LEFT JOIN 
    (SELECT OrderNumber, COUNT(*) AS CountofSentinelTests 
     FROM TblSentinelTestSheet
     GROUP BY OrderNumber) s
  ON j.OrderNumber = s.OrderNumber)
LEFT JOIN 
    (SELECT OrderNumber, COUNT(*) AS CountofHUVTests
     FROM TblHUVTestSheet
     GROUP BY OrderNumber) h
  ON j.OrderNumber = h.OrderNumber

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...