473,774 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INNER JOIN and MAX between two tables

30 New Member
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 5419
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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
1545
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, Orders.Product, Order.Price FROM Customers INNER JOIN Orders ON Customers.cust_id =3D Orders.cust_id WHERE cust_id =3D 2;
3
3355
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 inner joins, as follows : SELECT DISTINCT upcards.id,statuskey.status,upcards.firstname,upcards.lastname,originkey.ori gin,associatekey.username,associatekey2.username,upcards.deleted FROM upcards,status,origins,associates INNER JOIN status...
3
6416
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, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
4
23913
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: Students, Subjects, and Tests. I am trying to create a query that joins these three tables and show all the subjects for only one student, and all the tests taken in each of those subjects. This is the query I have entered in the Query Builder: ...
6
9315
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: tblEmployees empId -- EmpName -- EmpRole -- EmpManager -------....------------.... ---------....--------------- 1........ dan yella..........1..........2
52
6351
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 variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
7
2571
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
16507
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 all data from the separate tables is shown in a view (instead of the reference id's pointing to the separate tables...) I have some troubles formulating the SQL statement: I tried:
3
2389
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. Here is the Query
0
2353
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 wondering if this way is more efficient then using a DataSet.Relations.Add approach or a combination of the two. When should you use an Inner Join over a Relation within a dataset and vice versa? For example is this possible: ...
0
9454
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
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.