473,503 Members | 8,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get Multiple Max Records

68 New Member
Good day,

Hope you can help me to build this query. I have two tables one is for the tasks and the other one is for trials (see description of tables below). Each employee is allowed to perform more than a trial per each task. In other words, each task could have more than one trial for any employee. What I want to get is the records in the Trials Table based on the latest or maximum Trial Number performed in each task for all the employees (which are lines# 2, 4&5 in the example below).

Example:

Trial ID Task ID Trial Number Employee Badge
1 50 1 277321
2 50 2 277321
3 50 1 280000
4 50 2 270000
5 60 1 277321


Description:

Tasks Table (Parent Table)
1- Task ID (Primary Key)
2- Task Title
Trials Table (Child Table)
1- Trial ID (Primary Key)
2- Task ID (Foreign Key)
3- Trial Number (this reflects how many times the task has been performed)
4- Employee Badge

Thanks in advance
Aug 29 '12 #1
11 1910
twinnyfo
3,653 Recognized Expert Moderator Specialist
I would use an aggregate query, grouping on the Employee Badge Number, the Task ID and the Max Trial Number.

In your set of records above, you should get:

EBN Task ID Trial Number
270000 50 2
277321 50 2
277321 60 1
280000 50 2

Is this the result you want?
Aug 29 '12 #2
zmbd
5,501 Recognized Expert Moderator Expert
try:
SQL:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    tbl_trials.badge,
  3.    Max(tbl_trials.trial_number) AS MaxOftrial_number
  4. FROM tbl_trials
  5. GROUP BY tbl_trials.badge;
Returns:
Expand|Select|Wrap|Line Numbers
  1. badge    MaxOftrial_number
  2. 270000    2
  3. 277321    2
  4. 280000    1
If you want to show trial_id too then that's a different bag of tricks.
-z
Aug 29 '12 #3
ahd2008
68 New Member
Thanks for your swift response gents. I regret to tell you that I have done a mistake while posting the question. Although I have double checked the post but couldn't spot that minor error. The error is in the fourth record; the badge number should be 280000 instead of 270000.

twinnyfo, thanks for your support. The problem with your suggestion is that when I want to show the Trial ID in the query, I don't get the required result.

zmbd, thanks for your help too. Your approach is almost what I need and I have tried that before. The only problem is as you mentioned I need to show the Trial ID but without affecting the results. Yes, please I need your bag of tricks :)

Thank you all again
Aug 29 '12 #4
Rabbit
12,516 Recognized Expert Moderator MVP
Take the aggregate query and join it back to the table to retrieve the other fields that you didn't group by.
Aug 29 '12 #5
zmbd
5,501 Recognized Expert Moderator Expert
Ahh Rabbit,
You spoiled my fun
:)

Of course, Rabbit is absolutely correct.
-z
Aug 29 '12 #6
twinnyfo
3,653 Recognized Expert Moderator Specialist
Yes.... I keep forgetting that one can only query one additional field when using the Max Function....
Aug 30 '12 #7
ahd2008
68 New Member
Good day all,

Thanks for everyone participated. Dear Rabbit, I cannot seem to have your suggestion worked out.

I built an aggregate query as directed by twinnyfo & zmbd, but when I joined it to the Trials table it didn't bring the required result.

This is the aggregate query to get the maximum tirals "Query1"

Expand|Select|Wrap|Line Numbers
  1. SELECT Trials.BadgeNo, Trials.TaskID, Max(Trials.[Trial Number]) AS [MaxOfTrial Number]
  2. FROM Trials
  3. GROUP BY Trials.BadgeNo, Trials.TaskID;
This is the second query that joins the query above with the Trials table "Query2"

Expand|Select|Wrap|Line Numbers
  1. SELECT Query1.BadgeNo, Query1.TaskID, Query1.[MaxOfTrial Number], Trials.[Trial ID]
  2. FROM Trials INNER JOIN Query1 ON Trials.TaskID = Query1.TaskID;
  3.  
This is result that I am looking for:


Expand|Select|Wrap|Line Numbers
  1. EBN   Task ID MaxOftrial_number Trial ID
  2. 270000 50              2            4
  3. 277321 50              2            2
  4. 277321 60              1            5
  5. 280000 50              1            3
  6.  
Thanks again
Aug 30 '12 #8
zmbd
5,501 Recognized Expert Moderator Expert
Close,

Try this...

Design, show the trials table, show the query
Pull the desired fields from trials table to the builder grid
in the table link between the table and query on the two related fields in the query
Show nothing of the query in the grid.
-z
Aug 30 '12 #9
twinnyfo
3,653 Recognized Expert Moderator Specialist
ahd2008,

Here is what I came up with:

qryMaxTrial:

Expand|Select|Wrap|Line Numbers
  1. SELECT Trials.[Employee Badge], Trials.[Task ID], Max(Trials.[Trial Number]) AS [MaxOfTrial Number]
  2. FROM Trials
  3. GROUP BY Trials.[Employee Badge], Trials.[Task ID];
  4.  
qryMaxTrialID

Expand|Select|Wrap|Line Numbers
  1. SELECT Trials.[Employee Badge], Trials.[Task ID], Trials.[Trial Number], Trials.[Trial ID]
  2. FROM qryMaxTrial INNER JOIN Trials ON (qryMaxTrial.[MaxOfTrial Number] = Trials.[Trial Number]) AND (qryMaxTrial.[Task ID] = Trials.[Task ID]) AND (qryMaxTrial.[Employee Badge] = Trials.[Employee Badge]);
  3.  
qryMaxTrialID produces this result:

Expand|Select|Wrap|Line Numbers
  1. Employee Badge Task ID Trial Number Trial ID
  2. 277321         50      2            2
  3. 280000         50      1            3
  4. 270000         50      2            4
  5. 277321         60      1            5
  6.  
Hope this helps............
Aug 30 '12 #10
zmbd
5,501 Recognized Expert Moderator Expert
so many ways to skin this cat...

Here's mine:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.    tbl_trials.[badge],
  3.    tbl_trials.[task_id], 
  4.    tbl_trials.[trial_number], 
  5.    tbl_trials.[trial_id]
  6. FROM tbl_trials 
  7.    INNER JOIN 
  8.       (SELECT 
  9.           tbl_trials.badge,
  10.           Max(tbl_trials.trial_number) 
  11.              AS MaxOftrial_number
  12.         FROM tbl_trials
  13.         GROUP BY tbl_trials.badge)  
  14.             AS qry_trials_maxoftrial 
  15.         ON (tbl_trials.badge = qry_trials_maxoftrial.badge) 
  16.             AND (qry_trials_maxoftrial.MaxOftrial_number = tbl_trials.trial_number);
1) Simple select query on the original table showing all of the fields,
2) Add the first aggregate query I posted
3) and join on the two returned fields from aggregate

And now you get:

Expand|Select|Wrap|Line Numbers
  1. badge    task_id    trial_number    trial_id
  2. 277321       50         2                2
  3. 280000       50         1                3
  4. 270000       50         2                4
(note: I didn't change the badgeid in the data table as this is just an example...)
-z
Aug 30 '12 #11
ahd2008
68 New Member
Many thanks for twinnyfo & zmbd. You have done great inspiring efforts and enlightend me a lot. Now it is working

Thanks again and best wishes to you all
Aug 31 '12 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

10
3201
by: shank | last post by:
I have a recordset that contains multiple records of product a user is purchasing. For clarity, I converted the recordset fields to variables. I need to take that entire recordset and insert it...
1
4731
by: andree | last post by:
Hello, I have a form where a user may paste multiple records. The form has an AfterInsert procedure. The procedure copies the newly inserted record into a different table for audit purposes. ...
2
4167
by: JMCN | last post by:
i am running into problems with is the recordset. i have exactly 104 records. when i tested the code, the end result was multiple records for the same loan(ET LN Shortname). where did i go wrong? ...
4
3672
by: musicloverlch | last post by:
I have a table with multiple records in it. I am being forced to combine multiple records into one record in order to be uploaded to a website, and I only get one record per client. How can I...
5
13951
by: Kaur | last post by:
Hi, I have been successful copying a vba code from one of your posts on how to copy and paste a record by declaring the desired fields that needs to be copied in form's declaration and creating two...
3
2140
by: chalrav | last post by:
Reading through multiple records, with Loop capability -------------------------------------------------------------------------------- Hi, I have three tables as below: Table: Demand...
4
9782
4Him
by: 4Him | last post by:
First off, let me say this is a great site! I've just started working with Access and much of my success is from what I've read here! Background: I have a form, driven off a single table. Goal:...
0
4432
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
8
4749
by: jmarcrum | last post by:
Hello all, i have a continuous form that displays about 1000 records and opens when I click a button. When the form opens, the user has the option of checking a checkbox that is located beside...
0
1447
by: t8ntboy | last post by:
ASP/SQL Server Express 05 I have two tables, A and B. I would like to insert multiple records into both form A and B, but a field in form B is the Record ID in form A. So, I need to insert the...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.