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 476159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:27:41+00:00 2026-05-13T00:27:41+00:00

Let’s assume we have a table Maintenance Customer LastLogin ActionType 1 12/1/2007 2 1

  • 0

Let’s assume we have a table Maintenance

Customer LastLogin ActionType
1        12/1/2007 2
1        12/2/2007 2
etc.

We want a list of all customers who at any point during a given year had one or more uninterrupted sequences, 14 days long, of login with action type 2.

I can of course easily do this with code, and even have it be fairly quick over small sets. Is there a non-cursor way to do it in SQL?

  • 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-05-13T00:27:41+00:00Added an answer on May 13, 2026 at 12:27 am

    This will select all customers with at least two consecutive actions of the same type.

    WITH    rows AS 
            (
            SELECT  customer, action,
                    ROW_NUMBER() OVER (PARTITION BY customer ORDER BY lastlogin) AS rn
            FROM    mytable
            )
    SELECT  DISTINCT customer
    FROM    rows rp
    WHERE   EXISTS
            (
            SELECT  NULL
            FROM    rows rl
            WHERE   rl.customer = rp.customer
                    AND rl.rn = rp.rn + 1
                    AND rl.action = rp.action
            )
    

    Here’s the more efficient query for just action 2:

    WITH    rows AS 
            (
            SELECT  customer, ROW_NUMBER() OVER (PARTITION BY customer ORDER BY lastlogin) AS rn
            FROM    mytable
            WHERE   action = 2
            )
    SELECT  DISTINCT customer
    FROM    rows rp
    WHERE   EXISTS
            (
            SELECT  NULL
            FROM    rows rl
            WHERE   rl.customer = rp.customer
                    AND rl.rn = rp.rn + 1
            )
    

    Update 2:

    To select uninterrupted ranges:

    WITH    rows AS 
            (
            SELECT  customer, action, lastlogin
                    ROW_NUMBER() OVER (PARTITION BY customer ORDER BY lastlogin) AS rn
                    ROW_NUMBER() OVER (PARTITION BY customer, action ORDER BY lastlogin) AS series
            FROM    mytable
            )
    SELECT  DISTINCT customer
    FROM    (
            SELECT  customer
            FROM    rows rp
            WHERE   action
            GROUP BY
                    customer, actioncode, series - rn
            HAVING
                    DETEDIFF(day, MIN(lastlogin), MAX(lastlogin)) >= 14
            ) q
    

    This query calculates two series: one returns contiguous ORDER BY lastlogin, the second one partitions by action additionally:

    action  logindate rn  series diff = rn - series
    1       Jan 01    1   1      0
    1       Jan 02    2   2      0
    2       Jan 03    3   1      2
    2       Jan 04    4   2      2
    1       Jan 05    5   3      2
    1       Jan 06    6   4      2
    

    As long as the difference between the two schemes is the same, the series are uninterrupted. Each interruption breaks the series.

    This means that the combination of (action, diff) defines the uninterrupted groups.

    We can group by action, diff, find MAX and MIN within the groups and filter on them.

    If you need to select 14 rows rather than 14 consecutive days, just filter on COUNT(*) instead of the DATEDIFF.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a table with a Color column. Color can have various
Let's say I have thousands of users and I want to make the passwords
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let assume we have two activities. A - main activity, that is home launcher
Let's say I have table with column 'URL' whrere I store urls like this
Let's assume I want to deserialize this (I've removed the namespaces to make things

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.