How can I get access to calculate good producers based on test results for each producer? A good producer will have their last 30 shipments pass specifications. I want access to show me the current streak of good loads for each producer. A producer is a 3 part variable: 'company name' & 'city' & 'state/province'. I can get access to identify a good load and bad load and the dates of shipments are in the database.
Kevin Robertson
@Reply 3 years ago
Screenshots?
Kevin Yip
@Reply 3 years ago
This is not a trivial problem, and we need some advanced query technique. Basically, you need a query that returns the top 30 records for each producer if the records are sorted by producer, then in descending order by inspection date; then you check if all those 30 records indicate passed inspections.
The query to select top 30 records is below, but it only works for one producer at a time. If the producers are identified by ID 100, 200, 300, etc., then you have to make a query for every one of them:
SELECT TOP 30 ProductID, DateInspected, Passed FROM InspectionsT WHERE ProducerID = 100;
SELECT TOP 30 ProductID, DateInspected, Passed FROM InspectionsT WHERE ProducerID = 200;
SELECT TOP 30 ProductID, DateInspected, Passed FROM InspectionsT WHERE ProducerID = 300;
Etc.
So how do we do this in one fell swoop? You need to use a nested query, i.e. a query within a query.
Suppose your inspection records are in a table named InspectionsT that looks like Figure 1 below. Each inspection is identified by a unique primary key PKID. To simplify my presentation a bit, let's say we want to check only the last 5 inspections for each producer. In this table, producer 100 passed the last 5 inspections. Producer 101 only passed the last 1 inspection, on 2/25/23. Producer 102 passed the last 8 inspections. Producer 103 is relatively new and only did 4 inspections so far, all passed. So in this example, you want Access to tell you only producer 100 and 102 passed their 5 previous inspections.
First, you need to make a query to return the last 5 inspections, pass or fail, of each producer. Figure 2 below shows this query in SQL view and design view. It is a "nested query" that also uses "table alias", two subjects that are quite advanced and involved. Other posters may point you to the courses on this site so you can see what level they belong to. The result of this query is as shown: all past 5 inspections of 100, 101, and 102 are listed, and 103 only has 4 inspections because that's all it has.
Based on results from the above query, you then need to make another query that counts the number of passes for each producer, and returns only the producers with 5 passes. This is also done via a nested query that uses a table alias, shown in Figure 3 below.
I will post some more here shortly to help you unpack all this a little more.
Kevin Yip
@Reply 3 years ago
Kevin Yip
@Reply 3 years ago
Kevin Yip
@Reply 3 years ago
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Visitor Forum.