Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Access Query joining to Max record in another table

Posted on 2016-10-04
8
Medium Priority
?
57 Views
Last Modified: 2016-10-05
Experts,

I'm trying to query two tables,
1. An employee base record table (_Ebase) (contains no duplicates)
2. An employee personal information table (ePerson)

ePerson has multiple records per employee because a new record is created every time they change address,  but I only want the latest record from the ePerson side.

For the full data set, I would join the tables on the eBase table unique ID for the employee (called FlxID in this system) where eBase.ebflxID = ePerson.epFlxIdEb (the Eb suffix means its the foreign key for EbFlxID)

How do I set up the query to only pull
1. Everyone from the eBase side
2. but only the last record from the ePerson side for each employee?

Intuitively, it would be the Max of the unique ID for the table (ePerson.EpFlxID) for each employee. I don't necessarily need the solution to be in Sql form (using the query design interface is fine also) but pasting the Sql here is easy to do so,

SELECT [_NRCEbase].EbFlxID, NRCEPerson.EpFlxID, [_NRCEbase].EbRecType, "19660" AS co, [_NRCEbase].EbClock AS id, "I" AS [Record Type], [_NRCEbase].EbLastName AS [Last Name], [_NRCEbase].EbFirstName AS [First Name], [_NRCEbase].EbMiddleName, Left([_NRCEbase]![EbSocNumber],3) & "-" & Mid([_NRCEbase]![EbSocNumber],4,2) & Right([_NRCEbase]![EbSocNumber],4) AS SSN, [_NRCEbase].EbDateBeg, NRCEPerson.EpDateBorn
FROM _NRCEbase INNER JOIN NRCEPerson ON [_NRCEbase].EbFlxID = NRCEPerson.EpFlxIDEb
WHERE ((([_NRCEbase].EbRecType)="EBAS"))
ORDER BY [_NRCEbase].EbLastName;

Open in new window

0
Comment
Question by:Ron
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41828931
try
SELECT Table1.Field1, qryMax.MaxOfscore
FROM Table1 INNER JOIN qryMax ON Table1.ID = qryMax.personID;

qrymax
SELECT Table2.personID, Max(Table2.score) AS MaxOfscore
FROM Table2
GROUP BY Table2.personID;
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41828933
sorry main query has a left join

SELECT Table1.Field1, qryMax.MaxOfscore
FROM Table1 LEFT JOIN qryMax ON Table1.ID = qryMax.personID;
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 41828937
if you want the last record (as opposed to the highest score) do max on the id (which was defined as autonumber) and group by person id.
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41829039
Can you tell me what issue are you facing with the query you have written?
0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41829058
Try this  .. SOLUTION 1

SELECT r.* , S.EpDateBorn FROM 
(
	SELECT
	   `E`.EbFlxI`, S.EpFlxID, `E`.EbRecTyp`, `19660` AS co, `E`.EbCloc` AS id, `I` AS [Record Type],
	  `E`.EbLastNam` AS [Last Name], `E`.EbFirstNam` AS [First Name], `E`.EbMiddleNam`, Left(`E`![EbSocNumber],3)
	  & `-` & Mid(`E`!`EbSocNumber`,4,2) & Right(`E`!`EbSocNumber`,4) AS SSN, `E`.EbDateBe`
	  ,(SELECT MAX(EpFlxIDEb) MaxEpFlxIDEb
		FROM NRCEPerson N
		WHERE E.personID = N.personID
		) AS MaxEpFlxIDEb
	FROM
	  _NRCEbase E
)r INNER JOIN NRCEPerson re ON r.MaxEpFlxIDEb = re.EbFlxID

Open in new window



or Solution 2..

SELECT
	   [E].EbFlxID, S.EpFlxID, [E].EbRecType, "19660" AS co, [E].EbClock AS id, "I" AS [Record Type],
	  [E].EbLastName AS [Last Name], [E].EbFirstName AS [First Name], [E].EbMiddleName, Left([E]![EbSocNumber],3)
	  & "-" & Mid([E]![EbSocNumber],4,2) & Right([E]![EbSocNumber],4) AS SSN, [E].EbDateBeg
	  ,(SELECT N.EpDateBorn
		FROM NRCEPerson N
		WHERE E.personID = N.personID
		ORDER BY N.EbFlxID DESC
		LIMIT 1
		) AS EpDateBorn
	FROM
	  _NRCEbase E

Open in new window


Enjoy !!
0
 

Author Comment

by:Ron
ID: 41830074
Will these approaches work if I need multiple columns, say 7 or 8, from the second table?

The task actually requires me to populate a template with 40 or so fields coming from 5 tables, 4 of of which are the many side of a 1 to many relationship. So, would it be better to create new queries for each of the 4 left joined tables that filter for only the records i need form those tables and then join them?
0
 
LVL 30

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41830118
Yes first method will work in that case. Added all columns from 2nd table. <below>

Yes you are correct about the second comment.

SELECT r.* , re.* FROM 
(
	SELECT
	   `E`.EbFlxI`, S.EpFlxID, `E`.EbRecTyp`, `19660` AS co, `E`.EbCloc` AS id, `I` AS [Record Type],
	  `E`.EbLastNam` AS [Last Name], `E`.EbFirstNam` AS [First Name], `E`.EbMiddleNam`, Left(`E`![EbSocNumber],3)
	  & `-` & Mid(`E`!`EbSocNumber`,4,2) & Right(`E`!`EbSocNumber`,4) AS SSN, `E`.EbDateBe`
	  ,(SELECT MAX(EpFlxIDEb) MaxEpFlxIDEb
		FROM NRCEPerson N
		WHERE E.personID = N.personID
		) AS MaxEpFlxIDEb
	FROM
	  _NRCEbase E
)r INNER JOIN NRCEPerson re ON r.MaxEpFlxIDEb = re.EbFlxID

Open in new window

0
 

Author Closing Comment

by:Ron
ID: 41830414
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question