Home » SQL & PL/SQL » SQL & PL/SQL » First datetime in a block
First datetime in a block [message #286423] |
Fri, 07 December 2007 06:01  |
VentureFree
Messages: 3 Registered: December 2007
|
Junior Member |
|
|
I've worked a pretty complicated problem down to this simpler one, which will help me unlock the more difficult one if I can figure it out.
Say I have a list of events, each timestamped with a datetime. Now, given an arbitrary datetime, I need to able to find the first record whose timestamp is less than one hour before the next timestamp in the record set, starting with the given datetime (of course). So, for instance if I had the following data:
'12/03/2007 3:40:11 AM'
'12/03/2007 5:29:13 AM'
'12/03/2007 5:59:11 AM'
'12/03/2007 6:26:01 AM'
'12/03/2007 7:19:38 AM'
'12/03/2007 7:43:48 AM'
'12/03/2007 9:44:17 AM'
And I was given the datetime '12/03/2007 7:00:00 AM', I need a query that will return the single record with the datetime of '12/03/2007 5:29:13 AM' (the second record in the set), since the record before that is more than an hour before it.
Is this even possible? If so, any example code would be greatly appreciated.
[Updated on: Fri, 07 December 2007 06:09] Report message to a moderator
|
|
|
|
|
|
Re: First datetime in a block [message #286441 is a reply to message #286438] |
Fri, 07 December 2007 07:04   |
 |
Michel Cadot
Messages: 68758 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Still don't unerstand you example.
If you ask '7:00', the next record is '7:19' and the one that is the closest but less than 1 hour before is '5:59' not '5:29'.
Still don't have a valid test case (create table and insert statements).
Still don't know your Oracle version.
Read and follow OraFAQ Forum Guide
Regards
Michel
[Updated on: Fri, 07 December 2007 07:05] Report message to a moderator
|
|
|
|
Re: First datetime in a block [message #286501 is a reply to message #286474] |
Fri, 07 December 2007 11:33   |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Alternatively, cut all that nonsense out and use the LAG function 
A quick example:
SQL> with dat as ( SELECT TO_DATE('12/03/2007 3:40:11 AM','mm/dd/yyyy hh:mi:ss am') t FROM dual UNION ALL
2 SELECT TO_DATE('12/03/2007 5:29:13 AM','mm/dd/yyyy hh:mi:ss am') FROM dual UNION ALL
3 SELECT TO_DATE('12/03/2007 5:59:11 AM','mm/dd/yyyy hh:mi:ss am') FROM dual UNION ALL
4 SELECT TO_DATE('12/03/2007 6:26:01 AM','mm/dd/yyyy hh:mi:ss am') FROM dual UNION ALL
5 SELECT TO_DATE('12/03/2007 7:19:38 AM','mm/dd/yyyy hh:mi:ss am') FROM dual UNION ALL
6 SELECT TO_DATE('12/03/2007 7:43:48 AM','mm/dd/yyyy hh:mi:ss am') FROM dual UNION ALL
7 SELECT TO_DATE('12/03/2007 9:44:17 AM','mm/dd/yyyy hh:mi:ss am') FROM dual )
8 SELECT t
9 ,LAG(t,1) OVER(ORDER BY t) previous
10 ,LEAD(t,1) OVER(ORDER BY t) next
11 FROM dat
12 ORDER BY t;
T PREVIOUS NEXT
---------------------- ---------------------- ----------------------
12/03/2007 03:40:11 am 12/03/2007 05:29:13 am
12/03/2007 05:29:13 am 12/03/2007 03:40:11 am 12/03/2007 05:59:11 am
12/03/2007 05:59:11 am 12/03/2007 05:29:13 am 12/03/2007 06:26:01 am
12/03/2007 06:26:01 am 12/03/2007 05:59:11 am 12/03/2007 07:19:38 am
12/03/2007 07:19:38 am 12/03/2007 06:26:01 am 12/03/2007 07:43:48 am
12/03/2007 07:43:48 am 12/03/2007 07:19:38 am 12/03/2007 09:44:17 am
12/03/2007 09:44:17 am 12/03/2007 07:43:48 am
[Updated on: Fri, 07 December 2007 11:44] Report message to a moderator
|
|
|
Re: First datetime in a block [message #286777 is a reply to message #286501] |
Mon, 10 December 2007 01:57   |
VentureFree
Messages: 3 Registered: December 2007
|
Junior Member |
|
|
DOH! I just realized that this is Oracle (you'd think I'd have noticed before now). Mostly I'm interested in the straight SQL, as in non-Oracle specific code. Please feel free to tell me to go to h*ll for wasting your time. BUT in case you don't mind, I think I know how to describe what I'm trying to do. Let me try this. Say I have the following:
CREATE TABLE Events ( UserID int, Timestamp datetime )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 3:22:25 AM’ )
INSERT INTO Events VALUES ( 280, ‘12/03/2007 3:24:41 AM’ )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 3:40:11 AM’ )
INSERT INTO Events VALUES ( 280, ‘12/03/2007 4:18:32 AM’ )
INSERT INTO Events VALUES ( 280, ‘12/03/2007 4:56:18 AM’ )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 5:29:13 AM’ )
INSERT INTO Events VALUES ( 280, ‘12/03/2007 5:34:28 AM’ )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 5:59:11 AM’ )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 6:26:01 AM’ )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 7:19:38 AM’ )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 7:43:48 AM’ )
INSERT INTO Events VALUES ( 345, ‘12/03/2007 11:44:17 PM’ )
(I put some extraneous info in to make it more realistic) Given this as input (UserID = 345, Timestamp = ‘12/03/2007 7:00:00 AM’), I would like a sql statement that returns the following:
345, ‘12/03/2007 5:29:13 AM’
345, ‘12/03/2007 5:59:11 AM’
345, ‘12/03/2007 6:26:01 AM’
345, ‘12/03/2007 7:19:38 AM’
345, ‘12/03/2007 7:43:48 AM’
You’ll note that each returned datetime is no more than 1 hour different from at least one other record in the set, and at least 1 record is no more than 1 hour different from the input time. I think I know how to check for each being within an hour of at least one other:
SELECT DISTINCT Current.UserID, Current.Timestamp
FROM Events as Current INNER JOIN Events as Previous
ON Current.UserID = Previous.UserID AND DateDiff(’h',Current.Timestamp, Previous.Timestamp) = 0
WHERE Current.UserID = @UserID
AND Current.Timestamp <> Previous.Timestamp
ORDER BY Current.Timestamp DESC;
As is this will essentially list all the events for that user, since every event will be within an hour of at least one other event (though not necessarily in my testcase). So how do I limit it to that block of time for which at least one of the records is within an hour of the given datetime? Is that even possible?
[Updated on: Mon, 10 December 2007 02:01] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Sat Jun 07 06:25:02 CDT 2025
|