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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:08:10+00:00 2026-05-17T15:08:10+00:00

Lets say, i have the following table | start | end | activity +———————+———————+———

  • 0

Lets say, i have the following table

| start               | end                 | activity
+---------------------+---------------------+---------
| 2010-07-26 09:10:00 | 2010-07-26 09:21:00 | 6
| 2010-07-26 09:35:00 | 2010-07-26 09:47:00 | 5
| 2010-07-26 10:05:00 | 2010-07-26 10:45:00 | 5
| 2010-07-26 10:50:00 | 2010-07-26 11:22:00 | 6
| 2010-07-26 13:15:00 | 2010-07-26 13:43:00 | 7
| 2010-07-26 14:12:00 | 2010-07-26 14:55:00 | 2

I want to combine the small time spans, getting the average minutes with activity per hour. Something like that:

| start               | minutes_activity    | avg_activity
+---------------------+---------------------+---------
| 2010-07-26 09:00:00 | 42                  | {avg value}
| 2010-07-26 10:00:00 | 50                  | {avg value}
| 2010-07-26 11:00:00 | 22                  | {avg value}
| 2010-07-26 13:00:00 | 28                  | {avg value}
| 2010-07-26 14:00:00 | 43                  | {avg value}

Note that some records can have activity minutes in two hours, i.e 10:50:00 – 11:22:00. In this case, 10 minutes should be added to 10:00 and 22 minutes to 11:00.

A solution, be it MySql, PHP or both is appreciated.

Thank you advance.

  • 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-17T15:08:11+00:00Added an answer on May 17, 2026 at 3:08 pm

    Let’s assume that your table is named records and that your time intervals don’t span through more than two hours:

    SELECT DISTINCT(CONCAT(date,' ',IF(LENGTH(hour)=1, CONCAT('0',hour), hour), ':00:00')) AS start,
           SUM(minutes) AS minutes_activity,
           FORMAT(avg(activity),0) AS avg_activity
    FROM (
          SELECT date, hour, minutes, activity
          FROM (
                SELECT DATE(start) AS date,
                       HOUR(ADDDATE(start, INTERVAL 1 HOUR)) AS hour,
                       IF(HOUR(start) < HOUR(end), MINUTE(end), 0) AS minutes,
                       activity AS activity
                FROM records
                HAVING minutes>0
               ) t1
          UNION (
                 SELECT DATE(ADDDATE(start, INTERVAL 1 DAY)) AS date,
                        HOUR(ADDDATE(start, INTERVAL 1 HOUR)) AS hour,
                        IF(HOUR(start) > HOUR(end), MINUTE(end), 0) AS minutes,
                        activity AS activity
                 FROM records
                 HAVING minutes>0
                        AND hour=0
                )
          UNION (
                 SELECT DATE(start) AS date,
                        HOUR(start) AS hour,
                        IF(HOUR(start) < HOUR(end),
                           (60 - MINUTE(start)),
                           IF(
                              DATE(start) < DATE(end),
                              (60 - MINUTE(start)),
                              (MINUTE(end) - MINUTE(start))
                           )
                        ) AS minutes,
                        activity AS activity
                 FROM records
                )
          ORDER BY date ASC, hour ASC) t2
    GROUP BY CONCAT(date,' ',IF(LENGTH(hour)=1, CONCAT('0',hour), hour), ':00:00');
    

    … however, if you have an interval that goes from e.g. 2010-10-08 01:30:00 to 2010-10-08 03:13:00, a (not so) simple SQL query won’t do it.

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

Sidebar

Related Questions

Lets say I have the following: <table><tr><td> <table> <!-- I want a reference to
Lets say I have table with following columns 1. Client - string. 2. Profit
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
Lets say I have a table with the following columns: Qty, INTEGER SaleDate, DATETIME
Lets say I have the following table User_ID Manager_ID --------------------- Linda Jacob Mark Linda
I have the following mysql table called test. lets say it has the data
Lets say I have a table with some data like the following: ID text
Lets say I have the following query. SELECT stringdata FROM table ORDER BY FIELDS(
Lets say I have the following table in MS SQL 2000 Id | description
Lets say we have a following table with two columns and following rows of

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.