473,569 Members | 2,793 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 1917
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

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

Similar topics

10
3212
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 into another table on a remote server. The below code only inserts 1 record. How do I change the code to get all records inserted? thanks! <% Dim...
1
4740
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. When one record is pasted, all is working fine. If multiple records are pasted, the AfterInsert event gets fired before the Paste Confirmation...
2
4169
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? so i tried a couple of different do... loop statements and ended up getting the same results, that is having multiple records of the same loan(ET...
4
3676
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 systematically combine multiple records into 1? The table looks like this Client ID Service Status 1 Doc Pending...
5
13967
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 button "copy" and "paste". Works like magic. My problem is how can I copy multiple records and paste them at the same time. My data entry form has...
3
2143
by: chalrav | last post by:
Reading through multiple records, with Loop capability -------------------------------------------------------------------------------- Hi, I have three tables as below: Table: Demand ITEM_ID, QUANTITY Item A, 10
4
9792
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: To have multiple fields copied and pasted into multiple records with a single button click. Work Done: In the form I have multiple fields, but for...
0
4437
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 tables are all linked with the field 'member_id', which is an auto-increment field in the parent table ('members'). I've been able to input multiple...
8
4767
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 each record. The checkbox is labeled "Move" (for moving multiple records at a time to another year). Of course, the checkbox is set as a control...
0
1462
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 records into A, collect the ID and then insert into B. I have no problem doing this for individual records; I am not sure what the best way to...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.