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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:59:12+00:00 2026-05-24T02:59:12+00:00

Need a query help. I have a table where data is represented like this

  • 0

Need a query help. I have a table where data is represented like this

userid   TransactionTime           FunctionKey
1018     2011-07-27 09:14:38.000   0
1018     2011-07-27 09:17:11.000   99
1018     2011-07-27 09:38:22.000   0
1018     2011-07-27 10:34:50.000   99

I need a query which will return a dataset like this:

userid  Intime                  Outtime
1018    2011-07-27 09:14:38.000 2011-07-27 09:17:11.000
1018    2011-07-27 09:38:22.000 2011-07-27 10:34:50.000 

Please Help..

  • 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-24T02:59:14+00:00Added an answer on May 24, 2026 at 2:59 am
    declare @T table (userid int, TransactionTime datetime, FunctionKey int)
    insert into @T values
    (1018,     '2011-07-27 09:14:38.000',   0),
    (1018,     '2011-07-27 09:17:11.000',   99),
    (1018,     '2011-07-27 09:38:22.000',   0),
    (1018,     '2011-07-27 10:34:50.000',   99),
    (1019,     '2011-07-27 09:14:38.000',   0),
    (1019,     '2011-07-27 09:17:11.000',   0),
    (1019,     '2011-07-27 09:38:22.000',   99),
    (1020,     '2011-07-27 09:14:38.000',   0),
    (1020,     '2011-07-27 09:17:11.000',   99),
    (1020,     '2011-07-27 09:38:22.000',   99)
    
    ;with cte as
    ( 
      select userid,
             TransactionTime,
             FunctionKey,
             row_number() over(partition by userid order by TransactionTime) as rn
      from @T
    )
    select C1.userid,
           C1.TransactionTime as InTime,
           C2.TransactionTime as OutTime
    from cte as C1
      left outer join cte as C2
        on C1.userid = C2.userid and
           C1.rn + 1 = C2.rn
    where C1.FunctionKey = 0
    

    Result:

    userid      InTime                  OutTime
    ----------- ----------------------- -----------------------
    1018        2011-07-27 09:14:38.000 2011-07-27 09:17:11.000
    1018        2011-07-27 09:38:22.000 2011-07-27 10:34:50.000
    1019        2011-07-27 09:14:38.000 2011-07-27 09:17:11.000
    1019        2011-07-27 09:17:11.000 2011-07-27 09:38:22.000
    1020        2011-07-27 09:14:38.000 2011-07-27 09:17:11.000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help writing a T-SQL query that will generate 52 rows of data
I have a query which fetches a column from a table whose data type
I have a query I need to perform on a table that is roughly
i have a input tag which is non editable, but some times i need
I have a snippet to create a 'Like' button for our news site: <iframe
I have table tblEvents with columns dEvent_Date, dEvent_Time, tEvent_Emp_Code_num I must group all dates
I have a table that has the user ID already in it, but some
I need to solve the following question which i can't get to work by
I need to develop a file indexing application in python and wanted to know
I am trying to load a html page through UIWebview.I need to disable all

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.