We help IT Professionals succeed at work.

T-SQL Returning nearest date

Medium Priority
1,093 Views
Last Modified: 2012-06-27
Hi. I'm trying to retrieve the unique ID's closest to today's date. So only ID's 1,2,3 that have the newest dates. See table below. Thanks.
----------------------------------------------
myDate                               ID
----------------------------------------------
4/02/2009 12:00:00 AM      1
1/05/2009 12:00:00 AM      1
3/07/2009 12:00:00 AM      3
6/07/2009 12:00:00 AM      3
8/07/2009 12:00:00 AM      1
8/07/2009 12:00:00 AM      2
Comment
Watch Question

Top Expert 2009

Commented:
Assuming all dates are in the past, this should work.

select id, max(myDate) from tbl
group by id;
Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
How are you defining nearest?  Within what interval?  In other words, has to be within a month ago or in the future or within a month either way or etc...
Top Expert 2009

Commented:
If not, just add a where clause:

select id, max(myDate) from tbl
where myDate < getdate()
group by id;
Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Or possibly you meant max (newest) date for each ID like MrJoltCola showed.  That would make sense, especially if your date format above is dd/MM/yyyy versus MM/dd/yyyy as I thought it was.
CERTIFIED EXPERT
Awarded 2008
Awarded 2008

Commented:
here are how you can find the dates closest to the current date...


select case when datediff(d, mindate, convert(varchar(10), getdate(), 101)) > datediff(d, maxdate, convert(varchar(10), getdate(), 101)) then maxdate else mindate end
from (
select as mindate = max(mydate) from tablename
where mydate < convert(varchar(10), getdate(), 101),
select as maxdate= min(mydate) from tablename
where mydate > convert(varchar(10), getdate(), 101)
) a
Top Expert 2007

Commented:
Since you have posted in sql server 2005, here you go.
As you can see, I have put some comments in case you need to vary it slightly to suit your needs.
select ID, myDate
from (
	select rn=ROW_NUMBER() over (order by myDate desc)
	where myDate <= GETDATE() -- closest (on or before) current date(time)
) X
where rn = 1 -- only one record for each unique ID

Open in new window

Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks,that gets the job done.
Top Expert 2009

Commented:
Hi, I am not objecting, but I am curious why this solution was chosen over the other valid ones?

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
I would agree.  That is a whole lot of code to just get ID and max date per ID which seems like what you wanted.  You would use the accepted approach if you had additional columns of data necessary that could not be aggregated.  If all you have is the column or columns that you are grouping by and columns that need aggregation like max(date) then http:#24799688 is much simpler (KISS principle).

Author

Commented:
I didn't want max date per ID, I wanted max date of only ID's 1,2,3. So if I added additional ID's like 4 & 5, then return only ID's 1,2,3,4,5. As you can see, there are mutliple ID's with different dates.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.