Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Query including date Expand / Collapse
Author
Message
Posted Tuesday, January 1, 2013 3:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 1, 2013 6:29 AM
Points: 6, Visits: 28
Hello

I want to select records such that if a user logs in again within 10 minutes it should ignore it, and only return the rows if the same user login after 10 or more minutes.

UserID LOGTIME
U1 25-12-2011 14:20:00
U1 25-12-2011 14:28:00 --ignore it
U1 25-12-2011 14:40:00
U1 25-12-2011 14:48:00 --ignore

note: datetime is in standard format, above values for example only
Post #1401566
Posted Tuesday, January 1, 2013 6:03 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 27, 2013 6:54 AM
Points: 40, Visits: 168
Hi,

Try:

with CTE_R as
(
select
t.*,
ROW_NUMBER() OVER(PARTITION BY UserID ORDER BY LOGTIME) as RowNum
from @MyTable as t
)

select
c.UserID,
c.LOGTIME
from CTE_R as c
left join CTE_R as a
on a.UserID = c.UserID and
a.RowNum = c.RowNum - 1
where DATEDIFF(MINUTE, a.LOGTIME, c.LOGTIME) > 10 or
c.RowNum = 1

Hope this helps.


http://www.imoveisemexposicao.com.br
Post #1401587
Posted Tuesday, January 1, 2013 6:08 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:49 PM
Points: 23,396, Visits: 32,222
qamar 52306 (1/1/2013)
Hello

I want to select records such that if a user logs in again within 10 minutes it should ignore it, and only return the rows if the same user login after 10 or more minutes.

UserID LOGTIME
U1 25-12-2011 14:20:00
U1 25-12-2011 14:28:00 --ignore it
U1 25-12-2011 14:40:00
U1 25-12-2011 14:48:00 --ignore

note: datetime is in standard format, above values for example only


Really could use a little more in the sample data and expected results area. Given the following, what would you expect?


UserID LogTime
U1 20111225 14:20:00
U1 20111225 14:28:00
U1 20111225 14:30:00
U1 20111225 14:31:00
U1 20111225 14:40:00
U1 20111225 14:48:00
U1 20111225 14:51:00
U2 20111225 14:20:00
U2 20111225 14:28:00
U2 20111225 14:33:00
U2 20111225 14:43:00
U2 20111225 14:53:00
U2 20111225 14:58:00
U2 20111225 15:09:00





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1401588
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse