473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

INNER JOIN and MAX between two tables

30
I have 2 tables that I need to use an INNER JOIN on.

tblEMPLOYEES has FIRSTNAME, LASTNAME, EMPILID, DATETRAINED, DEPT

tblMAIN has EMPILID, OBSERVATIONDATE

In tblMAIN, the EMPILID is not the primary key, and each ID can have several dates. I only want the last date each ID has an Observation, and then to join that with the tblEMPLOYEES data and essentially get a recordset of each employee, and their last observation date. I was playing around with some queries and this is what I have so far, but I get an error in the FROM section.

Expand|Select|Wrap|Line Numbers
  1. SELECT     EMP.EMPILID, EMP.FIRSTNAME, EMP.LASTNAME, EMP.DATETRAINED, EMP.DEPT, EMP.LASTOBSDATE
  2. FROM         tblEMPLOYEES AS EMP INNERJOIN
  3.                           (SELECT     MAIN.EMPILID, MAX(MAIN.OBSERVATIONDATE) AS LASTOBSDATE
  4.                             FROM          tblMAIN AS MAIN
  5.                             GROUP BY MAIN.EMPILID) AS COMB ON EMP.EMPILID = COMB.EMPILID
  6. GROUP BY EMP.EMPILID
I know the select to get the last observation is correct as I ran just that select command and it worked fine, just the join is the issue it seems, and I am fairly n00bish at SQL.

Can anyone see where I'm going wrong?
Jun 9 '08 #1
1 5373
Stewart Ross
2,545 Expert Mod 2GB
Hi. It is simpler to use the Access query editor to create the query for you, adding the two tables to the editor window, dragging the fields across. Use View Totals to allow grouping and calculations, and use Max for your observation date, giving the calculated field an appropriate alias name.

The resulting SQL does not use a subquery:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblEMPLOYEES.EMPILID, tblEMPLOYEES.FIRSTNAME, tblEMPLOYEES.LASTNAME, tblEMPLOYEES.DATETRAINED, tblEMPLOYEES.DEPT, Max(tblMAIN.OBSERVATIONDATE) AS MAXOBSDATE
  2. FROM tblEMPLOYEES INNER JOIN tblMAIN ON tblEMPLOYEES.EMPILID = tblMAIN.EMPILID
  3. GROUP BY tblEMPLOYEES.EMPILID, tblEMPLOYEES.FIRSTNAME, tblEMPLOYEES.LASTNAME, tblEMPLOYEES.DATETRAINED, tblEMPLOYEES.DEPT;
-Stewart
Jun 9 '08 #2

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

Similar topics

0
by: B. Fongo | last post by:
------=_NextPart_000_0011_01C36322.2FEF5DC0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable It works!=20 SELECT Customers.Name, Customers.City,...
3
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
4
by: Nathan | last post by:
I have an application that uses an Access database to gather information on students' test scores. In the database there are three tables which are joined by one- to-many relationships: ...
6
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: ...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
7
by: Christian Muenscher | last post by:
Dear group! The following statement gives me a resultset containing two columns containing a PersonID: SELECT * FROM A LEFT JOIN B ON A.PersonID=B.PersonID UNION SELECT * FROM A
3
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so...
3
by: Anila | last post by:
Hi Friends, My problem with Inner join is ... first i joined two tables and i got the result. after that iam trying to join one more table its giving syn tax error in JOIN condition. ...
0
by: katupilar | last post by:
I am writing a tool to interface with a couple of tables in SQL Server 2000. I usually write my queries with an Inner Join to bring in fields from seperate tables and load that to a DataSet. I'm...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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...

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.