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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:48:15+00:00 2026-05-24T00:48:15+00:00

I want to create an SQL Query for a SQLite3 DB. In my table

  • 0

I want to create an SQL Query for a SQLite3 DB.

In my table I have the following sample data

7/1/2011   8:00   LOGIN
7/1/2011   10:12  LOGOFF
7/1/2011   10:20  LOGIN
7/1/2011   17:15  LOGOFF
7/2/2011   9:00   LOGIN
7/3/2011   10:00  LOGIN
7/3/2011   20:00  LOGOFF

basically I want to only get the first LOGIN entry per date and the last LOGOFF entry for the same date (if available)

So I want the query to produce

7/1/2011   8:00   LOGIN
7/1/2011   17:15  LOGOFF
7/2/2011   9:00   LOGIN
7/3/2011   10:00  LOGIN
7/3/2011   20:00  LOGOFF

Is is also possible to calculate the difference in Time with the desired output table?

IE/ on 7/1/2011 would be 9:15

  • 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-24T00:48:16+00:00Added an answer on May 24, 2026 at 12:48 am

    Supposing your columns are named Date, Time, Action, this is what you need:

    select Date,
           case Action 
             when 'LOGIN' then Min(Time)
             when 'LOGOFF' then Max(Time)
           end as ActionTime,
           Action 
    from YourTable
    group by Date, Action
    

    Update

    For calculating the difference, you might want to “flatten” the rows of a day to a single row:

    select Date, MinLoginTime, MaxLogoffTime, MaxLogoffTime - MinLoginTime as Diff
    from
    (
    select Date,
           Min(
               case Action 
                   when 'LOGIN' then Time
                   else null
               end
           ) as MinLoginTime,
           Max(
               case Action 
                   when 'LOGOFF' then Time
                   else null
               end
           ) as MaxLogoffTime
    from YourTable
    group by Date
    ) YourDT
    

    Some considerations:

    • If you don’t have MinLoginTime OR MaxLogoffTime, Diff will be null
    • You will probably have to work on formatting Diff. Do some research on that for SQLite, every RDBMS has its particularities on that matter.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in an MS SQL Server db. I want to create
I need to create an SQL query to insert some data into a table
I have certain sqlite data having the certain code. String sql = create table
I want to create a list of columns in SQL Server 2005 that have
I want to create a table in MS SQL Server 2005 to record details
I want to create the following T-SQL statement: SELECT SUM (sa.Amount) as 'SumAmount', SUM(sa.Cost)
I have an application where I create a big SQL query dynamically for SQL
I need help with SQL. I have an sqlite table like so; CREATE TABLE
Using Django 1.1, how could I create a crosstab (pivot table) SQL query using
I want to create SQL query that returns some values and one of those

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.