Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Microsoft SQL Server > select cases wh...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 10966 of 11205
Post > Topic >>

select cases where a datetime is not 3 or more hours older than a

by Morten <morten@[EMAIL PROTECTED] > Apr 15, 2008 at 02:34 PM

Hello 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




 3 Posts in Topic:
select cases where a datetime is not 3 or more hours older than
Morten <morten@[EMAIL   2008-04-15 14:34:32 
Re: select cases where a datetime is not 3 or more hours older t
"Plamen Ratchev"  2008-04-15 10:14:21 
Re: select cases where a datetime is not 3 or more hours older t
Morten <morten@[EMAIL   2008-04-16 11:14:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Sat Jul 5 4:58:18 CDT 2008.