0

Is it possible to calculate data within specific times of the day for a certain period?

I have a large set of data and want to calculate the "Amount" collected between certain time of the day for a specific period.

For example: Below, I want to sum the amount collected between 00:00 - 14:59 as 'Amount 1' and amount collected between 15:00 - 20:59 as 'Amount 2' from the 1st of July to the 31st of July. 'Total Amount' = 'Amount 1' + 'Amount 2'

sum(if(Date between '2012-07-01 00:00:00' and '2012-07-31 14:59:00',(Amount),0)) as 'Amount 1', sum(if(Date between '2012-07-01 15:00:00' and '2012-07-31 20:59:00',(Amount),0)) as 'Amount 2', sum(Amount) as 'Total Amount'

However, the result for 'Total Amount' does not match 'Amount 1'+'Amount 2'.

3
  • The difference between Amount1+Amount2 and TotalAmount is caused by rows which are neither within the first nor the second date range. You are aware that the two ranges overlap significantly, right? You should probably clamp the dates with a WHERE clause and the times within the IF() constructs.
    – jensgram
    Aug 7, 2012 at 16:07
  • Thanks Jensgram! Since the date column contains both date and time, had to use the TIME() function and specified the dates in the "Where" clause. There are still some discrepancies, but will try to figure it out. sum(if((Time(DATE) between '15:00:00' and '20:59:00',(Amount),0)) as 'Amount 1'
    – Justus
    Aug 7, 2012 at 16:38
  • However, facing another problem! When I use Time between '00:00:00' - '14:59:59' and '15:00:00' and '20:59:59', it works. But If I change the time to show amount between '21:00:00'-'14:59:59'(next day) and '15:00:00' -'20:59:59', 'Amount 1' does not return any value. Here's the query: SELECT CUSTOMER ID, NAME, sum(if(Time(DATE) between '21:00:00' and '14:59:59',(Amount),0)) as 'Amount 1', sum(if(Time(DATE) between '15:00:00' and '20:59:59',(Amount),0)) as 'Amount 2', sum(Amount) as 'Total Amount' FROM CUSTOMER_DATABASE WHERE DATE between '2012-07-01' and '2012-07-31' group by 1 order by 5
    – Justus
    Aug 7, 2012 at 17:10

1 Answer 1

1

You are not putting any restrictions on your total amount field (i.e. no date or time restrictions, so data outside your date/time ranges is being included. You could either add conditional logic to your sum(Amount) like you did for the other fields or add a WHERE clause like this:

WHERE DATE(Amount) BETWEEN '2012-07-01' and '2012-07-31'
AND TIME(Amount) BETWEEN '00:00:00' and '20:59:00'

This would allow your Total Amount to be calculated correctly and also allow you to remove the date range condition in sore time range summation logic.

5
  • Sorry, should have included the complete query. I have specified the start and end date. SELECT CUSTOMER ID, NAME, sum(if(DATE between '2012-07-27 00:00:00' and '2012-07-31 14:59:00',(Amount),0)) as 'Amount 1', sum(if((DATE between '2012-07-27 15:00:00' and '2012-07-31 20:59:00',(Amount),0)) as 'Amount 2', sum(Amount) as 'Total Amount' FROM CUSTOMER_DATABASE WHERE DATE between '2012-07-27 00:00:00' and '2012-07-31 20:59:00' group by 1 order by 5
    – Justus
    Aug 7, 2012 at 16:24
  • Thanks for your help Mike! Figured it out :)
    – Justus
    Aug 7, 2012 at 16:45
  • However, facing another problem! When I use Time between '00:00:00' - '14:59:59' and '15:00:00' and '20:59:59', it works. But If I change the time to show amount between '21:00:00'-'14:59:59'(next day) and '15:00:00' -'20:59:59', 'Amount 1' does not return any value. Here's the query: SELECT CUSTOMER ID, NAME, sum(if(Time(DATE) between '21:00:00' and '14:59:59',(Amount),0)) as 'Amount 1', sum(if(Time(DATE) between '15:00:00' and '20:59:59',(Amount),0)) as 'Amount 2', sum(Amount) as 'Total Amount' FROM CUSTOMER_DATABASE WHERE DATE between '2012-07-01' and '2012-07-31' group by 1 order by 5
    – Justus
    Aug 7, 2012 at 17:28
  • @Justus That is because TIME(Date) BETWEEN '21:00:00' and '14:59:59' would not be a valid interval. The MySQL time function just strips out the time portion of a data time string without regard for the day component. It won't look ahead a day like that.
    – Mike Brant
    Aug 7, 2012 at 17:41
  • @Justus If this is what you were needing, you can accept the answer. It will help build your reputation to where people are more likely to help answering future questions.
    – Mike Brant
    Aug 7, 2012 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.