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 2005 Forums
 Transact-SQL (2005)
 Next record based on datetime

Author  Topic 

mlevier
Starting Member

33 Posts

Posted - 2007-08-07 : 09:04:47
I am trying to find out what the best SQL statement would be to find the next record based on a datetime field and grouped by telephone numbers.

This is important because I need to know when the next call is within 24 hours. If the call is within the 24 hours then the Rep from the first call gets a 1 added in the repeat calls field. So I need a way to only find the next call in by that telephone number.

nr
SQLTeam MVY

12543 Posts

Posted - 2007-08-07 : 09:24:49
Maybe not the fastest but this is one way

select t1.*, t2.*
from calls t1
left join calls t2
on t1.number = t2.number
and t2.calldate = (select min(t.calldate) from calls t where t.number = t1.number and t.calldate > t1.calldate)



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -