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

  • Home
  • SEARCH
  • 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 779121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:52:38+00:00 2026-05-14T19:52:38+00:00

I have this table UserID SessionID SessionStart SessionEnd ———————————————– 1 abc1 2010-1-1 2010-1-2 5

  • 0

I have this table

UserID   SessionID   SessionStart   SessionEnd
-----------------------------------------------
1        abc1        2010-1-1       2010-1-2
5        def3        2010-1-5       2010-1-9
1        llk0        2010-1-10      2010-1-11
5        spo8        2010-1-13      2010-1-15
1        pie7        2010-1-16      2010-1-29

I would like to be able to find the days between the end of one session to the start of the next session for each particular user.

So I am looking to get something like

UserID   DaysBetweenSessions
-----------------------------
1        8
1        5
5        4

Thanks!

  • 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-14T19:52:39+00:00Added an answer on May 14, 2026 at 7:52 pm

    Try:

    DECLARE @YourTable table (UserID int, SessionId char(5), SessionStart datetime, SessionEnd datetime)
    INSERT @YourTable VALUES (1,'abc1','2010-1-1 ','2010-1-2')
    INSERT @YourTable VALUES (5,'def3','2010-1-5 ','2010-1-9')
    INSERT @YourTable VALUES (1,'llk0','2010-1-10','2010-1-11')
    INSERT @YourTable VALUES (5,'spo8','2010-1-13','2010-1-15')
    INSERT @YourTable VALUES (1,'pie7','2010-1-16','2010-1-29')
    
    ;WITH AllStarts AS
    (SELECT
         UserID, SessionEnd,row_number() over (partition by UserID order by SessionStart) as EndRank
         FROM @YourTable
    )
    , AllEnds AS 
    (SELECT 
         UserID, SessionStart, row_number() over (partition by UserID order by SessionEnd) as StartRank
         FROM @YourTable
    )
    SELECT 
        s.UserID, DATEDIFF(day,s.SessionEnd,ISNULL(e.SessionStart,GETDATE())) AS DaysBetweenSessions
        FROM AllStarts              s
            LEFT OUTER JOIN AllEnds e on s.UserID = e.UserID and e.StartRank=s.EndRank+1
        --WHERE e.UserID is not NULL  --include to remove "ones in progress"
    

    OUTPUT:

    UserID      DaysBetweenSessions
    ----------- -------------------
    1           8
    1           5
    1           103
    5           4
    5           117
    
    (5 row(s) affected)
    

    if you don’t want to include the ones with out a matching next row (uncomment the WHERE) and get this result set:

    UserID      DaysBetweenSessions
    ----------- -------------------
    1           8
    1           5
    5           4
    
    (3 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]
I have two tables like this: Table Name: users emx | userid --------------- 1
This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded
I have a table which logs the userid, course, sessionid and requestdate each time
Lets say I have a session table like this: [Session] ------- Id: int UserId:
I have this table: User id INT PK login VARCHAR UNIQUE I want to
I have a couple of tables which look like this Table 1 user_id |
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have a table like this: a | user_id ----------+------------- 0.1133 | 2312882332 4.3293
I have the following tweets table: tweet_id user_id text --------------------------------------------------- 1 2 this is

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.