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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:39:26+00:00 2026-05-14T08:39:26+00:00

Running SQL Server 2008, and I am definitely a new SQL user. I have

  • 0

Running SQL Server 2008, and I am definitely a new SQL user.

I have a table that has 4 columns:

EmpNum, User, Action, Updatetime

A user logs into, and out of a system, it is registered in the database. For example, if user1 logs into the system, then out 5 minutes later, a simple query (select * from update) would look like:

EmpNum   User    Action   Updatetime
1        User1   I        2010-01-01 23:00:00:000
1        User1   O        2010-01-01 23:05:00:000

I’m trying to query the Empnum, User, Action, I(in time), O(out time), and the total time.

  • 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-14T08:39:27+00:00Added an answer on May 14, 2026 at 8:39 am
    Select T1.EmpNum, T1.User, T1.UpdateTime As TimeIn
     , (Select Min(T2.UpdateTime)
          From Table As T2
          Where T2.EmpNum = T1.EmpNum
          And T2.User = T1.User
          And T2.Action = 'O'
     And T2.UpdateTime > T1.UpdateTime) As TimeOut
    From Table As T1
    Where Action = 'I'
    

    EDIT In your comments you asked for TimeWorked. Since you are using SQL Server 2008, you can use an Outer Apply to calculate the out-time for each in-time row. Then it is a simple matter of using DateDiff.

    Select T1.EmpNum, T1.User
        , T1.UpdateTime As TimeIn
        , TimeOut.UpdateTime As TimeOut
        , DateDiff(mi, T1.UpdateTime, TimeOut.UpdateTime) As TimeWorkedInMinutes
    From Table As T1
        Outer Apply (
                    Select Min(T2.UpdateTime) As UpdateTime
                    From Table As T2
                    Where T2.EmpNum = T1.EmpNum
                        And T2.User = T1.User
                        And T2.Action = 'O'
                        And T2.UpdateTime > T1.UpdateTime
                    ) As TimeOut
    Where Action = 'I'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It could be because the text displayed in the link… May 16, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer How about: getattr(foo.__class__, <field_name>) This should give you the field… May 16, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer See the functions save and load. May 16, 2026 at 2:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Edit: Im running SQL Server 2008 I have about 400,000 rows in my table.
Using SQL Server 2008, I have a requirement that email addresses in my user
I'm running SQL Server 2008 Express on Windows XP on a VirtualPC instance inside
Scenario: Production servers are running SQL Server 2008 in the domain myDomain.com Dev/test/stage servers
I'm running SQL Server 2008, and trying to run some queries on some poorly
I am running sql server 2008 express and i need to schedule some stored
I am running SQL Server Reporting Services on SQL Server 2008 Standard and trying
Our web app's db server (running SQL Server 2008) is starting to get pounded
We have a legacy database which is a sql server db (2005, and 2008).
I am running SQL Server and I have a stored procedure. I want do

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.