Solved

MS SQL 2000 Query

Posted on 2009-04-12
3
213 Views
Last Modified: 2012-05-06
I have two tables with one to many relationship namley employees (Emp) and qualifications (Qual). I want to select all employees with the most recent qualification. Each employee might have more than one qualification. The two tables are joined by the emp_cpr column. I tried the following SQL query :

SELECT Emp.CPR, Emp.name, Max(Qual.Date) AS Date
FROM Emp INNER JOIN Qual ON emp.cpr = Qual.cpr
GROUP BY Emp.cpr,emp.name

It's working  only if I select two columns from the first table namely emp_cpr and emp_name. I want to group all employees by cpr showing only thier latest qualification.

Thanks
0
Comment
Question by:AZZA-KHAMEES
  • 2
3 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24126215
SELECT e.cpr, q.QualDescr --guessing on the column name here!
FROM Emp e INNER JOIN
      Qual q ON e.cpr = q.CPR INNER JOIN
      (SELECT q2.cpr, Max(q2.Date) AS MaxDate
      FROM Qual q2
      GROUP BY q2.cpr) z ON q.cpr = z.cpr AND q.Date = z.MaxDate
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 24126220
Sorry, make that


SELECT e.cpr, e.Name, q.QualDescr --guessing on the column name here!
FROM Emp e INNER JOIN
      Qual q ON e.cpr = q.CPR INNER JOIN
      (SELECT q2.cpr, Max(q2.Date) AS MaxDate
      FROM Qual q2
      GROUP BY q2.cpr) z ON q.cpr = z.cpr AND q.Date = z.MaxDate
0
 

Author Closing Comment

by:AZZA-KHAMEES
ID: 31569339
Thanks alot. It's working fine
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

803 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