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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:03:38+00:00 2026-05-24T05:03:38+00:00

In MSSQL 2008 I have table and data which looks like this create table

  • 0

In MSSQL 2008 I have table and data which looks like this

create table #tempData
(user_id int,type varchar(10),ts datetime)

insert into #tempData
select 1,'ENTER','2011-01-30 15:00:00'
union all
select 1,'EXIT','2011-01-31 16:00:00'
union all
select 1,'ENTER','2011-02-1 18:00:00'
union all
select 1,'EXIT','2011-02-10 21:00:00'
union all
select 2,'ENTER','2011-01-10 21:00:00'
union all
select 2,'EXIT','2011-01-12 21:00:00'
union all
select 2,'ENTER','2011-01-13 01:00:00'
union all
select 2,'EXIT','2011-01-13 18:00:00'
--AND SO ON --

Now I trying to make query which is going to tell how long one user was inside according to last exit
something like

user_id,exited,time_in_hours
1,'2011-01-31 16:00:00',25 
1,'2011-02-10 21:00:00',219
2,'2011-01-12 21:00:00',48
2,'2011-01-13 18:00:00',17

Sorry if this BAD or ROUGE question and if this is not proper way to ask a question like this.
But I am stacked on this for while.

  • 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-24T05:03:39+00:00Added an answer on May 24, 2026 at 5:03 am

    Assuming that

    • same-user sessions do not intersect,

    • there cannot be an EXIT record without the corresponding ENTER record,

    you could try the following:

    WITH ranked AS (
      SELECT
        user_id,
        type,
        ts,
        rn = ROW_NUMBER() OVER (PARTITION BY user_id, type ORDER BY ts)
      FROM #tempData
    )
    SELECT
      tx.user_id,
      exited = tx.ts,
      hours = DATEDIFF(HOUR, tn.ts, tx.ts)
    FROM ranked tn
      INNER JOIN ranked tx ON tn.user_id = tx.user_id AND tn.rn = tx.rn
    WHERE tn.type = 'ENTER'
      AND tx.type = 'EXIT'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in my database that looks like this: Date (DateTime) Type (varchar)
I have a database for an E-commerce storefront. MSSQL 2008. I have a table
I want to have a database table that keeps data with revision history (like
I have a table of data which represents a series of events that persons
I have a ms sql 2008 table with data from a weather datalogger. table
I have a php script which accesses a MSSQL2005 database, reads some data from
I have a table containing user input which needs to be optimized. I have
MSSQL 2008. I am trying to construct a SQL statement which returns the total
I have created a sql function in SQLServer 2008 that declared a temporary table
I have: Table with columns A int, B int, C int I need to

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.