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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:47:46+00:00 2026-05-13T13:47:46+00:00

I have a table of events, each with a StartTime and EndTime (as type

  • 0

I have a table of events, each with a StartTime and EndTime (as type DateTime) in a MySQL Table.

I’m trying to output the sum of overlapping times and the number of events that overlapped.

What is the most efficient / simple way to perform this query in MySQL?

CREATE TABLE IF NOT EXISTS `events` (
  `EventID` int(10) unsigned NOT NULL auto_increment,
  `StartTime` datetime NOT NULL,
  `EndTime` datetime default NULL,
  PRIMARY KEY  (`EventID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=37 ;


INSERT INTO `events` (`EventID`, `StartTime`, `EndTime`) VALUES
(10001, '2009-02-09 03:00:00', '2009-02-09 10:00:00'),
(10002, '2009-02-09 05:00:00', '2009-02-09 09:00:00'),
(10003, '2009-02-09 07:00:00', '2009-02-09 09:00:00');


# if the query was run using the data above,
# the table below would be the desired output

# Number of Overlapped Events | Total Amount of Time those events overlapped.
1, 03:00:00
2, 02:00:00
3, 02:00:00

The purpose of these results is to generate a bill for hours used. (if you have one event running, you might pay 10 dollars per hour. But if two events are running, you only have to pay 8 dollars per hour, but only for the period of time you had two events running.)

  • 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-13T13:47:46+00:00Added an answer on May 13, 2026 at 1:47 pm

    Try this:

    SELECT `COUNT`, SEC_TO_TIME(SUM(Duration))
    FROM (
        SELECT
            COUNT(*) AS `Count`,
            UNIX_TIMESTAMP(Times2.Time) - UNIX_TIMESTAMP(Times1.Time) AS Duration
        FROM (
            SELECT @rownum1 := @rownum1 + 1 AS rownum, `Time`
            FROM (
                SELECT DISTINCT(StartTime) AS `Time` FROM events
                UNION
                SELECT DISTINCT(EndTime) AS `Time` FROM events
            ) AS AllTimes, (SELECT @rownum1 := 0) AS Rownum
            ORDER BY `Time` DESC
        ) As Times1
        JOIN (
            SELECT @rownum2 := @rownum2 + 1 AS rownum, `Time`
            FROM (
                SELECT DISTINCT(StartTime) AS `Time` FROM events
                UNION
                SELECT DISTINCT(EndTime) AS `Time` FROM events
            ) AS AllTimes, (SELECT @rownum2 := 0) AS Rownum
            ORDER BY `Time` DESC
        ) As Times2
        ON Times1.rownum = Times2.rownum + 1
        JOIN events ON Times1.Time >= events.StartTime AND Times2.Time <= events.EndTime
        GROUP BY Times1.rownum
    ) Totals
    GROUP BY `Count`
    

    Result:

    1, 03:00:00
    2, 02:00:00
    3, 02:00:00
    

    If this doesn’t do what you want, or you want some explanation, please let me know. It could be made faster by storing the repeated subquery AllTimes in a temporary table, but hopefully it runs fast enough as it is.

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

Sidebar

Related Questions

I have a MySQL table containing a listing of events that I am trying
I have a SQL table Events (ID int, Event int, StartTime datetime, Duration int
I have a database table of events. Each event has a category and a
Suppose I have a table events {id, userid, create_time, country, type, page, browser} I
I have a mySQL table of events and want to display them in a
I have a table of events, and another of ratings. Each event can have
I have a MYSQL table that holds events information. I need a mechanism that
I have a table which lists all events on a debating calendar. Each row
I have two tables called 'events' and 'topics' each table can have many comments.
I have a table of Events and a number of subtables of different types

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.