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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:24:46+00:00 2026-06-09T02:24:46+00:00

Problem – Retrieve sum of subtotals on a half hour interval efficiently I am

  • 0

Problem – Retrieve sum of subtotals on a half hour interval efficiently

I am using MySQL and I have a table containing subtotals with different times. I want to retrieve the sum of these sales on a half hour interval from 7 am through 12 am. My current solution (below) works but takes 13 seconds to query about 150,000 records. I intend to have several million records in the future and my current method is too slow.

How I can make this more efficient or if possible replace the PHP component with pure SQL? Also, would it help your solution to be even more efficient if I used Unix timestamps instead of having a date and time column?

Table Name – Receipts

subtotal    date        time      sale_id
--------------------------------------------
   6        09/10/2011  07:20:33     1
   5        09/10/2011  07:28:22     2
   3        09/10/2011  07:40:00     3
   5        09/10/2011  08:05:00     4
   8        09/10/2011  08:44:00     5
...............
  10        09/10/2011  18:40:00     6
   5        09/10/2011  23:05:00     7

Desired Result

An array like this:

  • Half hour 1 ::: (7:00 to 7:30) => Sum of Subtotal is 11
  • Half hour 2 ::: (7:30 to 8:00) => Sum of Subtotal is 3
  • Half hour 3 ::: (8:00 to 8:30) => Sum of Subtotal is 5
  • Half hour 4 ::: (8:30 to 9:00) => Sum of Subtotal is 8

Current Method

The current way uses a for loop which starts at 7 am and increments 1800 seconds, equivalent to a half hour. As a result, this makes about 34 queries to the database.

for($n = strtotime("07:00:00"), $e = strtotime("23:59:59"); $n <= $e; $n += 1800) {  

    $timeA = date("H:i:s", $n);
    $timeB = date("H:i:s", $n+1799);

    $query = $mySQL-> query ("SELECT SUM(subtotal)
                              FROM Receipts WHERE time > '$timeA' 
                              AND time < '$timeB'");

    while ($row = $query-> fetch_object()) {
        $sum[] = $row;
    }
}

Current Output

Output is just an array where:

  • [0] represents 7 am to 7:30 am
  • [1] represents 7:30 am to 8:00 am
  • [33] represents 11:30 pm to 11:59:59 pm.

    array (“0” => 10000,
    “1” => 20000,
    …………..
    “33” => 5000);

  • 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-09T02:24:47+00:00Added an answer on June 9, 2026 at 2:24 am

    You can try this single query as well, it should return a result set with the totals in 30 minute groupings:

    SELECT date, MIN(time) as time, SUM(subtotal) as total
    FROM `Receipts`
    WHERE `date` = '2012-07-30'
    GROUP BY hour(time), floor(minute(time)/30)
    

    To run this efficiently, add a composite index on the date and time columns.

    You should get back a result set like:

    +---------------------+--------------------+
    | time                | total              |
    +---------------------+--------------------+
    | 2012-07-30 00:00:00 |        0.000000000 |
    | 2012-07-30 00:30:00 |        0.000000000 |
    | 2012-07-30 01:00:00 |        0.000000000 |
    | 2012-07-30 01:30:00 |        0.000000000 |
    | 2012-07-30 02:00:00 |        0.000000000 |
    | 2012-07-30 02:30:00 |        0.000000000 |
    | 2012-07-30 03:00:00 |        0.000000000 |
    | 2012-07-30 03:30:00 |        0.000000000 |
    | 2012-07-30 04:00:00 |        0.000000000 |
    | 2012-07-30 04:30:00 |        0.000000000 |
    | 2012-07-30 05:00:00 |        0.000000000 |
    | ...
    +---------------------+--------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have a table that prints out vertical but I would like it
Problem Using Director 11.5 and Windows 7, with MouseWheel Xtra (wheelmouse.zip), I have the
Problem PHP session in different folder. I have problem with PHP session. There are
Problem description I have 6 databases from 6 different machines, and having one cloud
Problem I have a Silverlight 5 application using the treeview from the SDK. Now
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem I have a strange problem. I have written an application in MFC(using VS2003)
Problem: I am trying to build a recursive tree using a function and data
Problem: I'm streaming my video from a php file with stream_get_contents(); using Flowplayer as
Problem: I have two array where one produce a category and the second produce

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.