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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:49:46+00:00 2026-05-17T23:49:46+00:00

I’m writing a query that will fetch results from a logging table, resulting in

  • 0

I’m writing a query that will fetch results from a logging table, resulting in a overview of how many visitors that were logged on my webpage at a specified interval, taking in account their actual login time (login_time, logout_time) and the input GROUP by parameter. (DAY(),HOUR() or MINUTE())

I’ve twisted my brain around this for a few days on and off. Really need some positive input from anyone interested.

Input

So basicly what I would input in my query would be what time interval I would like to search by. Specifying how the result would be grouped like. (DAY(),HOUR() or MINUTE())

Example data

ID| userID | login_time          | logout_time
------------------------------------------------------
1 | 1      | 06.11.2010 16:57:16 | 06.11.2010 16:34:11
2 | 2      | 06.11.2010 16:47:11 | 06.11.2010 19:55:15
3 | 3      | 06.11.2010 16:33:16 | 06.11.2010 16:53:33
4 | 4      | 06.11.2010 16:13:25 | 06.11.2010 18:54:54
5 | 5      | 06.11.2010 16:02:16 | 06.11.2010 16:34:11
6 | 6      | 06.11.2010 16:00:11 | 06.11.2010 17:55:19
7 | 6      | 06.11.2010 19:00:11 | 06.11.2010 22:55:19
8 | 6      | 06.11.2010 20:00:11 | 06.11.2010 23:55:19
9 | 6      | 06.11.2010 20:00:11 | 06.11.2010 21:55:19
9 | 6      | 06.11.2010 09:00:11 | 06.11.2010 10:00:19

Prefered result

Example Input: Between 06.11.2010 16:30:00 and 06.11.2010 16:35:00 and grouped by MINUTE()

Count | Date
---------------------------
5     | 06.11.2010 16:30:00
5     | 06.11.2010 16:31:00
5     | 06.11.2010 16:32:00
4     | 06.11.2010 16:33:00
5     | 06.11.2010 16:34:00
3     | 06.11.2010 16:35:00

Example Input: Between 06.11.2010 00:30:00 and 06.11.2010 23:35:00 and grouped by HOUR()

Count | Date
---------------------------
1     | 06.11.2010 09:00:00
1     | 06.11.2010 10:00:00

–> PS: I don’t mind having a gap here if there is intervals missing (ie. no data)

6     | 06.11.2010 16:00:00
3     | 06.11.2010 17:00:00
2     | 06.11.2010 18:00:00
2     | 06.11.2010 19:00:00
3     | 06.11.2010 20:00:00
3     | 06.11.2010 21:00:00
2     | 06.11.2010 22:00:00
1     | 06.11.2010 23:00:00

Maybe you could illustrate a suitable and effective query that will yield the expected results I have in mind? I’ll leave my cluttering code out from this post for the moment.

  • 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-17T23:49:46+00:00Added an answer on May 17, 2026 at 11:49 pm

    The easiest way to do this in SQL is to create a temporary table with all the time values you want to test, and then join it to the log table.

    drop table if exists minutes;
    create temporary table minutes (date datetime not null);
    set @t := '2010.11.06 16:30:00';  insert into minutes values (@t);
    set @t := @t + interval 1 minute; insert into minutes values (@t);
    set @t := @t + interval 1 minute; insert into minutes values (@t);
    set @t := @t + interval 1 minute; insert into minutes values (@t);
    set @t := @t + interval 1 minute; insert into minutes values (@t);
    set @t := @t + interval 1 minute; insert into minutes values (@t);
    
    select count(*) as count, m.date
    from minutes m join log l
    on m.date <= l.logout_time and m.date + interval 1 minute >= l.login_time
    group by m.date;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.