November 7, 2008 at 10:24 am
Hi, I know there are great ways to solve this in SQL 2005 and beyond, but this server is SQL 2000:
Given a table with the following:
code date ...
24 5/15/08
24 6/12/07
26 1/15/02
27 7/24/08
27 5/01/05
How can I return just the top 1 date (decending aka most recent) for each code?
November 7, 2008 at 10:33 am
You would use the MAX() aggregate function.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
November 7, 2008 at 10:33 am
yep. That would do it. I'm going to hide under my desk now. Thanks!!!
November 7, 2008 at 10:35 am
Is this what you are after?
/*
code date ...
24 5/15/08
24 6/12/07
26 1/15/02
27 7/24/08
27 5/01/05
*/
create table #TestTab (
code int,
codedate datetime
);
insert into #TestTab
select 24, '2008-05-15' union all
select 24, '2007-06-12' union all
select 26, '2002-01-15' union all
select 27, '2008-07-24' union all
select 27, '2005-05-01';
select
code,
max(codedate)
from
#TestTab
group by
code;
drop table #TestTab;
November 7, 2008 at 10:35 am
Glad I could help.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply