简体   繁体   中英

How do I select a summed range of columns from MySQL within a 24 hour period?

I have a significant amount of data in which I am trying to calculate the sum of one column based on occurrences within a 24 hour period.

The table is as follows:

ID          Customer    LOCATION  FROM_DATETIME     TO_DATETIME       Amount    24Revenue

23100470    Customer01  Idaho   2014-03-23 23:40    2014-03-24 00:00    8.20    26.65
23111106    Customer01  Idaho   2014-03-24 07:06    2014-03-24 07:26    0.00    22.14
23111865    Customer01  Idaho   2014-03-24 07:27    2014-03-24 07:30    0.00    22.14
23112909    Customer01  Idaho   2014-03-24 07:56    2014-03-24 08:06    3.69    22.14
23122768    Customer01  Idaho   2014-03-24 11:38    2014-03-24 11:46    3.28    18.45
23125519    Customer01  Idaho   2014-03-24 12:35    2014-03-24 12:38    1.23    15.17
23126385    Customer01  Idaho   2014-03-24 12:53    2014-03-24 13:00    2.87    13.94
23141213    Customer01  Idaho   2014-03-24 19:58    2014-03-24 20:16    7.38    11.07
23155172    Customer01  Idaho   2014-03-25 06:52    2014-03-25 07:01    3.69    3.69
23165719    Customer02  Idaho   2014-03-25 10:52    2014-03-25 13:34    44.97   70.21
23173857    Customer02  Idaho   2014-03-25 13:36    2014-03-25 13:43    2.87    25.24
23174971    Customer02  Idaho   2014-03-25 14:00    2014-03-25 14:04    1.64    22.37
23176607    Customer02  Idaho   2014-03-25 14:40    2014-03-25 14:52    4.51    20.73
23178488    Customer02  Idaho   2014-03-25 15:24    2014-03-25 16:04    0.00    16.22
23181743    Customer02  Idaho   2014-03-25 16:42    2014-03-25 17:45    16.22   16.22

At this time I am choosing a data set for a period of 1 week:

SELECT ID, Customer, from_datetime, to_datetime, location 
WHERE to_datetime BETWEEN '2014-03-24 00:00:00' AND '2014-03-31' 
AND location='Idaho';

This query returns $recordset.

Then using that dataset witin PHP, I am looping through each record and then querying the database again to sum the revenue column for all records with the same plate and that occurred within 24 hours of that time:

foreach ($recordset as $key => $value) {

$fromdate=date('Y-m-d H:i:s',strtotime($rsRentals[$key]['to_datetime']));

$todate=strtotime(date('Y-m-d H:i:s', strtotime($fromdate)) . " +24 hours");


SELECT SUM(amount) as revenue WHERE plate='$recordset[$key]['plate']'
AND location = 'Idaho' AND to_datetime >= '$fromdate' AND from_datetime < '$todate'

}

My issue is that based on my calculations (50 sums within 2 minutes), it will take approximately 13-14 hours to sum ~20000 records. This is way too long and I think there must be a simpler way to run this query with a JOIN or something, but my attempts have failed. Above is the only setup that I have been able to do with success, albeit a limited dataset.

Does anyone have suggestions on what I could do to obtain the same result but a much quicker and efficient query?

use the first query to grab the list of plates,

then insert that as a subquery to a bigger query where your plate is your group by and your where in the outer querty is where time is between TO_DATETIME and TO_DATETIME-24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM