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.
| Author |
Topic |
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2008-03-08 : 16:18:14
|
| If I want to find other events that occur within a specified time frame of one event, what would the logic be?Please consider the following:2 tables:Booking BEvents EFields:b.bookingide.reqtimeI want to find all B.Bookingid that have an e.reqtime +/ 15 minutes from eachother.I currently use the following query to find all b.bookingid with the same e.reqtime...it would be helpful to find other bookings within a 15 minute window of eachother.Currently use:SELECT e.reqtime, count(b.bookingid) as 'Trips'From Booking b JOIN events e ON e.bookingid=b.bookingid and e.activity=0Where b.ldate=?Group by e.reqtimehaving count(e.reqtime)>=2Order by e.reqtimeThanks Much! |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-08 : 17:02:44
|
Here is sample code and an answer to demonstrate Create Table #bookings (BookingID int identity (1,1),Textcolumn char(1) not null)Create Table #events (BookingID int not null, reqtime datetime not null)Insert INTO #bookings (textcolumn)Select 'A'From master..spt_valuesWhere type = 'P' and number between 1 and 10Insert into #events (bookingid,reqtime)Select 1,'1/1/2008 1:00:00.000 PM' UNION ALLSelect 2,'1/1/2008 12:45:00.000 PM' UNION ALLSelect 3,'1/1/2008 2:15:00.000 PM' UNION ALLSelect 4,'1/1/2008 7:25:00.000 PM' UNION ALLSelect 5,'1/1/2008 7:10:00.000 PM' UNION ALLSelect 6,'1/1/2008 8:00:00.000 PM' UNION ALLSelect 7,'1/1/2008 8:30:00.000 PM' UNION ALLSelect 8,'1/1/2008 2:45:00.000 PM' UNION ALLSelect 9,'1/1/2008 3:00:00.000 PM' UNION ALLSelect 10,'1/1/2008 2:00:00.000 PM' --SQL 2000Select identity(int, 1,1) as RowNum, BookingId, reqtimeINTO #orderedeventsFROM #eventsOrder by reqtime ascSelect e1.BookingID, e1.ReqTime,e2.BookingID as Conflict,e2.ReqTime as ConflictTimeFrom #orderedevents e1 ,#orderedevents e2 Where ABS(datediff(mi,e1.ReqTime,e2.ReqTime)) <=15 AND e1.BookingID != e2.BookingIDorder by BookingID--SQL 2005;WITH cteOrdered AS( SELECT ROW_NUMBER() OVER (ORDER BY ReqTime desc) AS RowNum, BookingID, ReqTime FROM #events)Select e1.BookingID, e1.ReqTime,e2.BookingID as Conflict,e2.ReqTime as ConflictTimeFrom cteOrdered e1 ,cteOrdered e2 Where ABS(datediff(mi,e1.ReqTime,e2.ReqTime)) <=15 AND e1.BookingID != e2.BookingIDorder by BookingIDdrop table #bookingsdrop table #eventsdrop table #orderedevents/*output from both is identical1 2008-01-01 13:00:00.000 2 2008-01-01 12:45:00.0002 2008-01-01 12:45:00.000 1 2008-01-01 13:00:00.0003 2008-01-01 14:15:00.000 10 2008-01-01 14:00:00.0004 2008-01-01 19:25:00.000 5 2008-01-01 19:10:00.0005 2008-01-01 19:10:00.000 4 2008-01-01 19:25:00.0008 2008-01-01 14:45:00.000 9 2008-01-01 15:00:00.0009 2008-01-01 15:00:00.000 8 2008-01-01 14:45:00.00010 2008-01-01 14:00:00.000 3 2008-01-01 14:15:00.000*/ Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2008-03-08 : 17:06:32
|
| DG,Thanks for the reply...reading over it, I have one problem. I'm only able to use select statements. The interface that I use is limited so users can't do update, delete, create or insert queries.I suppose this will not be possible simply using a Select query?Thanks |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-08 : 17:18:44
|
The create and inserts were only to generate sample data for which to provide a result, and test with so that I provide a working answer.If you are SQL 2000, you can't use the method without being able to create a temp table (or use too many resources)It is possible using a select query...but if you have a lot of rows...not very effective due to http://www.sqlservercentral.com/articles/T-SQL/61539/(something I just learned about courtesy of Jeff Moden)Sample batch without removing the triangular join. Many apologies to Jeff Moden, in advance. If you have a LOT of rows, this can be nasty to do.Create Table #bookings (BookingID int identity (1,1),Textcolumn char(1) not null)Create Table #events (BookingID int not null, reqtime datetime not null)Insert INTO #bookings (textcolumn)Select 'A'From master..spt_valuesWhere type = 'P' and number between 1 and 10Insert into #events (bookingid,reqtime)Select 1,'1/1/2008 1:00:00.000 PM' UNION ALLSelect 2,'1/1/2008 12:45:00.000 PM' UNION ALLSelect 3,'1/1/2008 2:15:00.000 PM' UNION ALLSelect 4,'1/1/2008 7:25:00.000 PM' UNION ALLSelect 5,'1/1/2008 7:10:00.000 PM' UNION ALLSelect 6,'1/1/2008 8:00:00.000 PM' UNION ALLSelect 7,'1/1/2008 8:30:00.000 PM' UNION ALLSelect 8,'1/1/2008 2:45:00.000 PM' UNION ALLSelect 9,'1/1/2008 3:00:00.000 PM' UNION ALLSelect 10,'1/1/2008 2:00:00.000 PM' --SQL 2000 not using creation of tablesSelect DISTINCT e1.BookingID, e1.ReqTime,e2.BookingID as Conflict,e2.ReqTime as ConflictTimeFROM #events as e1 , #events as e2Where ABS(datediff(mi,e1.ReqTime,e2.ReqTime)) <=15 AND e1.BookingID != e2.BookingIDorder by BookingIDdrop table #bookingsdrop table #eventsdrop table #orderedevents/*results1 2008-01-01 13:00:00.000 2 2008-01-01 12:45:00.0002 2008-01-01 12:45:00.000 1 2008-01-01 13:00:00.0003 2008-01-01 14:15:00.000 10 2008-01-01 14:00:00.0004 2008-01-01 19:25:00.000 5 2008-01-01 19:10:00.0005 2008-01-01 19:10:00.000 4 2008-01-01 19:25:00.0008 2008-01-01 14:45:00.000 9 2008-01-01 15:00:00.0009 2008-01-01 15:00:00.000 8 2008-01-01 14:45:00.00010 2008-01-01 14:00:00.000 3 2008-01-01 14:15:00.000*/ In 2005, you can use the CTE example, otherwise..you should have the idea from what I have posted (3 working solutions based on your original post) Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
|
|
|
|
|