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 function that brings me back a value by query a table
I have XML (see below). I need to insert the records in this XML
In this sample console app I want to update a row in a table,
user_table user_id | username 1 | solomon 2 | muna message table id user_id|
I have recently started learning SQL, I can't determine what is causing my syntax
I have created a sign-up and login for my website and all validation works
my application has the following entity: public class User { public virtual int UserID
We have an application that needs to access a database that is owned by
A feature that is currently missing from one of my web apps is that
I need to determine if user has already visited a page, for tracking unique

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.