SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Query including date


SQL Query including date

Author
Message
qamar 52306
qamar 52306
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 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
imex
imex
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 199
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97099 Visits: 38988
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



Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search