select cases where a datetime is not 3 or more hours older than asimilar case
am 15.04.2008 14:34:32 von MortenHello SQL-experts,
I have a table with a datetime field, 'accesstime'. The table also
holds a varchar column, 'IPaddress". I now need to select all the IP
addresses of this table where this datetime field is not represented
within 3 hours of another, similar entry.
Here's what I've got:
This is my table:
CREATE TABLE [dbo].[OlapWebUseLogEntries](
[PresentationID] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL,
[AccessTime] [datetime] NOT NULL,
[IPAddress] [bigint] NOT NULL,
[PageCalled] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_testTable] PRIMARY KEY CLUSTERED
(
[PresentationID] ASC,
[AccessTime] ASC,
[IPAddress] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Example of table content: (the bigint is the IP-address)
PresentationNo1;15-04-2008 15:17:28;2130706433;foo.aspx
PresentationNo1;15-04-2008 15:17:38;2130706433;foo.aspx
PresentationNo1;15-04-2008 15:17:41;2130706433;foo.aspx
PresentationNo1;15-04-2008 15:17:43;2130706433;foo2.aspx
PresentationNo2;15-04-2008 15:25:48;2195982562;foo3.aspx
PresentationNo2;15-04-2008 15:27:46;2195982562;foo2.aspx
PresentationNo1;15-04-2008 15:29:35;2195982562;foo3.aspx
PresentationNo1;15-04-2008 15:30:09;2195982562;foo.aspx
PresentationNo1;15-04-2008 15:53:41;2195982548;foo.aspx
PresentationNo1;15-04-2008 15:53:44;2195982548;foo.aspx
So in the above, I would like to select the first case, but not the
second and not the third - as the difference in datetime value to the
first is less than three hours to the first. Case no. four is also fine,
as its 'PageCalled' value is different from the first two. And so on and
so forth.
My best try was the below:
SELECT PresentationID, AccessTime, IPAddress, PageCalled
FROM OlapWebUseLogEntries AS foo
WHERE (AccessTime > '14-04-2008')
AND (AccessTime < '16-04-2008')
AND ( NOT EXISTS
(SELECT PresentationID, AccessTime, IPAddress, PageCalled
FROM OlapWebUseLogEntries AS bar
WHERE (PresentationID = foo.PresentationID) AND (IPAddress
= foo.IPAddress) AND (foo.PageCalled = PageCalled) AND
(DATEDIFF(hh, AccessTime, foo.AccessTime) < 3)))
ORDER BY AccessTime
Alas this doesn't seem the right way to go. Any clues on how to go about
this would surely be appreciated. Thank a lot in advance,
best,
Morten