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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:45:57+00:00 2026-06-15T16:45:57+00:00

I have a table in which I’m recording logins/logouts. Schema and sample data: http://sqlfiddle.com/#!2/e1b35/1

  • 0

I have a table in which I’m recording logins/logouts. Schema and sample data: http://sqlfiddle.com/#!2/e1b35/1

I need to create stored procedure which will print sum of (logout-login) times for an user and date (duration of using application for user on date). In column type I represents login and O logout.

Input parameters: @username, @date. Output: 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-06-15T16:45:58+00:00Added an answer on June 15, 2026 at 4:45 pm

    You need to identify all groups of logins that are related to each other. The way I would do this is by finding the logout associated with logins. You are dealing with log data, so don’t be surprised if there are multiple logins with no logout.

    select l.*,
           (select min(l2.time) from logs l2 where l2.username = l.username and l2.type = 'O' and l2.time > l.time
           ) as logoutTime
    from logs l
    where l.type = 'I'
    

    Now, you can use the username and LogoutTime as a pair for aggregation to get what you want:

    select username, logoutTime, min(time) as StartTime, logouttime as EndTime,
           datediff(s, min(time), logoutTime) as TimeInSeconds
    from (select l.*,
                 (select min(l2.time) from logs l2 where l2.username = l.username and l2.type = 'O' and l2.time > l.time
                 ) as logoutTime
          from logs l
          where l.type = 'I'
         ) l
    group by username, logoutTime
    

    Note that you specify SQL Server as a tag, but the SQL Fiddle is for MySQL; date functions tend to differ among databases.

    And, if you are using SQL Server 2012, there is an easier way. You should specify that if it is the case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which has been populated with incorrect data, so I need
I have table which contains double values stored in mysql database ...I need to
I have a table which stores the data related to posts in this format
I have a table which contains TV Guide data. In a simplified form, the
I have table which has a class data and it has table rows inside
I have table which is filled from external application. I need to fill column
I have one table which display data as from Dynamic Content, I want to
I have table data which looks like this id | keyword | count |
I have table which contains data along with created on date. I want to
I Have a table which holds date but I need to know the latest

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.