Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select data from most recent date

Author  Topic 

ygeorge
Yak Posting Veteran

68 Posts

Posted - 2004-12-02 : 15:00:19
Hi,

My Project table is like this -

PrjID EmpID Date PrjName
1 1 10/01/2004 P1
2 2 08/02/2004 P2
3 2 11/12/2004 P3
4 2 05/05/2004 P5
5 3 05/30/2004 P6
6 3 09/10/2004 P7

......

I need to use one query to return the most recent project for each employee. The return should like the following -

PrjID EmpID Date PrjName
1 1 10/01/2004 P1
4 2 11/12/2004 P5
6 3 09/10/2004 P7

Can anybody help me? Thanks in advance.

George

X002548
Not Just a Number

15586 Posts

Posted - 2004-12-02 : 15:36:50
You sure this ain't homework...and your desired result set I think is incorrect


USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99(PrjID int, EmpID int, [Date] datetime, PrjName char(2))
GO

INSERT INTO myTable99(PrjID, EmpID, [Date], PrjName)
SELECT 1, 1, '10/01/2004', 'P1' UNION ALL
SELECT 2, 2, '08/02/2004', 'P2' UNION ALL
SELECT 3, 2, '11/12/2004', 'P3' UNION ALL
SELECT 4, 2, '05/05/2004', 'P5' UNION ALL
SELECT 5, 3, '05/30/2004', 'P6' UNION ALL
SELECT 6, 3, '09/10/2004', 'P7'
GO

SELECT *
FROM myTable99 o
WHERE EXISTS (SELECT *
FROM myTable99 i
GROUP BY EmpId
HAVING MAX(i.[Date]) = o.[Date]
AND i.EmpID = o.EmpID)
GO

SET NOCOUNT OFF
DROP TABLE myTable99
GO




Brett

8-)
Go to Top of Page

ygeorge
Yak Posting Veteran

68 Posts

Posted - 2004-12-02 : 15:59:35
Thanks. That's exactly what I was looking for.

George
Go to Top of Page

Hippi
Yak Posting Veteran

63 Posts

Posted - 2004-12-17 : 14:51:11
quote:
Originally posted by ygeorge

Hi,

My Project table is like this -

PrjID EmpID Date PrjName
1 1 10/01/2004 P1
2 2 08/02/2004 P2
3 2 11/12/2004 P3
4 2 05/05/2004 P5
5 3 05/30/2004 P6
6 3 09/10/2004 P7

......

I need to use one query to return the most recent project for each employee. The return should like the following -

PrjID EmpID Date PrjName
1 1 10/01/2004 P1
4 2 11/12/2004 P5
6 3 09/10/2004 P7

Can anybody help me? Thanks in advance.

George


How about
select * from Mytable99 where
Date=(select max(date) from Mytable99 m1 where Mytable99.Empid=m1.empid)
order by empid
Go to Top of Page

bwright611
Starting Member

3 Posts

Posted - 2004-12-21 : 08:36:19
I have a question similar to the most recent date... What if I need the most recent date for a certain combination.. such as:

Person DayOfWeek Achievement AchievementDate
Brad Monday Went Out 12/1/04
Brad Tuesday Drove 12/14/04
Brad Monday Walked 12/5/04
Brad Tuesday Talked 12/15/04
Sallie Monday Wrote 11/4/04
Sallie Monday Drove 12/4/04

So the results will be:

Person DayOfWeek Achievement AchievementDate
Brad Monday Walked 12/5/04
Brad Tuesday Talked 12/15/04
Sallie Monday Drove 12/4/04

Thanks much,
BW


Go to Top of Page
   

- Advertisement -