Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:03:03+00:00 2026-06-01T16:03:03+00:00

i have a set of login data for a user_id with a time stamp.

  • 0

i have a set of login data for a user_id with a time stamp.

a user could login multiple times but we need to return records at least an hour apart from one another, starting from the min record. the deduping has to happen at a user level (there can be multiple users)

for eg.

  • user1 2012-03-07 14:24:30.000
  • user1 2012-03-07 14:34:30.000
  • user1 2012-03-07 15:14:30.000
  • user1 2012-03-07 15:20:30.000
  • user1 2012-03-07 15:30:30.000
  • user1 2012-03-08 09:20:30.000
  • user1 2012-03-08 09:50:30.000
  • user1 2012-03-08 10:30:30.000
  • user2 2012-03-07 15:20:30.000

i would only want to see the following records

  • user1 2012-03-07 14:24:30.000
  • user1 2012-03-07 15:30:30.000
  • user1 2012-03-08 09:20:30.000
  • user1 2012-03-08 10:30:30.000
  • user2 2012-03-07 15:20:30.000

========================================================================

is there any way to do this in a clean way? we could do this recursively but i was hoping there might be a way to use row_number partition by.

any help is much appreciated!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:03:04+00:00Added an answer on June 1, 2026 at 4:03 pm

    In Sql Server 2005 or newer this CTE will return table of LoginAt datetimes removing the ones less than hour apart from already selected LoginAts.

    ;with SkipHour(UserID, LoginAT, rn) as (
      select UserID, min(LoginAt), cast (1 as bigint)
        from LogTable
       group by UserID
      union all
      select SkipHour.UserID, LogTable.LoginAt,
             row_number() over (partition by SkipHour.UserID 
                                order by Logtable.LoginAt) rn
      from SkipHour
         inner join LogTable
            on LogTable.UserID = SkipHour.UserID
         where datediff(minute, SkipHour.LoginAt, LogTable.LoginAt) >= 60
         -- Only first rows from previous generation qualify to have children
            and rn = 1
    )
    select *
    from SkipHour
    where rn = 1
    order by UserID, LoginAT
    

    Crucial part is row_number(). As Sql Server does not allow neither aggregate functions nor top predicate, row_number() is the only way (IMO) to order loginAt datetimes and keep only first one.

    Sql Fiddle playground is this way.

    UPDATE:

    Row numbers are applies for each generation individually. Extract from WITH common_table_expression (Transact-SQL):

    Analytic and aggregate functions in the recursive part of the CTE are
    applied to the set for the current recursion level and not to the set
    for the CTE. Functions like ROW_NUMBER operate only on the subset of
    data passed to them by the current recursion level and not the entire
    set of data pased to the recursive part of the CTE. For more
    information, see J. Using analytical functions in a recursive CTE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have set up a login page that verifies the user's credentials, and
I have a login window with a Sign In button which is set as
I have a login page written in classic asp. I want to set the
I have set the Timeout property of SmtpClient class, but it doesn't seem to
I'm trying to set up a simple login using AuthLogic into my User table.
I have a small cakePHP app that allows for user's to login and be
I have an object that contains all login data, that's in my controller (it
We have a central login that we use to support multiple websites. To store
I have a web app in which I have set the maximum inactivity time
I have some logic that depends upon two properties being set, as it executes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.