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)
 How to select 3 most recent visit?

Author  Topic 

SQLCM
Starting Member

13 Posts

Posted - 2007-05-25 : 20:02:27
How do I select 3 most recent visit date for each client if the result from the following query returns more than 3 visit dates for each client

SELECT*
FROM client_table

Result:
last_Name visit_date
smith 2006-01-15
smith 2006-02-12
smith 2006-04-16
smith 2006-05-12
smith 2006-09-15
smith 2006-02-19
kelly 2006-02-13
kelly 2006-03-09
kelly 2006-06-03
kelly 2006-09-11
kelly 2006-02-13
kelly 2006-05-06
kelly 2006-01-13

The result I am looking for:
last_Name visit_date
smith 2006-09-15
smith 2006-05-12
smith 2006-04-16
kelly 2006-09-11
kelly 2006-06-03
kelly 2006-05-06

Thank you



khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-25 : 20:55:26
[code]
select *
from client_table c
where visit_date in (select top 3 visit_date from client_table x where x.last_name = c.last_name order by visit_date desc)
[/code]


KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-05-26 : 01:15:12
Also refer point 2
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SQLCM
Starting Member

13 Posts

Posted - 2007-05-29 : 12:34:28
Thank you very much for both of your suggestions!
Go to Top of Page
   

- Advertisement -