How do I select the hours difference between the min start and max end of a given date period?

user2048672

I've got a simple problem, but I'm having a difficult time trying to formulate the proper SQL query to get this done. I've got a table that has multiple dates. Each date has multiple times. The times are in start - end increments that are manually added by the user.

+----+------------------+------------------+
| id | start            | end              |
+----+------------------+------------------+
|  6 | 2014/03/03 08:30 | 2014/03/03 09:00 |
|  7 | 2014/03/03 09:00 | 2014/03/03 11:15 |
|  8 | 2014/03/03 11:15 | 2014/03/03 11:45 |
|  9 | 2014/03/03 11:45 | 2014/03/03 12:45 |
| 10 | 2014/03/03 12:45 | 2014/03/03 13:15 |
| 11 | 2014/03/04 08:45 | 2014/03/04 09:00 |
| 12 | 2014/03/04 09:00 | 2014/03/04 13:00 |
| 13 | 2014/03/04 13:00 | 2014/03/04 13:30 |
| 14 | 2014/03/05 09:00 | 2014/03/05 09:30 |
| 15 | 2014/03/05 09:30 | 2014/03/05 12:30 |
| 16 | 2014/03/05 12:45 | 2014/03/05 12:45 |
| 19 | 2014/03/06 08:45 | 2014/03/06 09:00 |
| 20 | 2014/03/06 09:15 | 2014/03/06 10:00 |

I'm trying to calculate the difference in hours between the earliest start time and the latest end time per date.

Currently I'm just using a simple unix_timestamp conversion on my rows, however, it's only calculating the difference between the first row found, instead of the difference of all rows for a given date.

select
    unix_timestamp(min(start)) - unix_timestamp(max(end)) / 60.0 / 60.0 as hours_difference,
    min(start) as started,
    min(end) as ended              
from
    athlete_log
group by
    day(start)

The problem (as stated above) is that it only grabs the first row from a given date, such as:

| 6 | 2014/03/03 08:30 | 2014/03/03 09:00 |

But I need the earliest start time and the latest start time from:

|  6 | 2014/03/03 08:30 | 2014/03/03 09:00 |
|  7 | 2014/03/03 09:00 | 2014/03/03 11:15 |
|  8 | 2014/03/03 11:15 | 2014/03/03 11:45 |
|  9 | 2014/03/03 11:45 | 2014/03/03 12:45 |
| 10 | 2014/03/03 12:45 | 2014/03/03 13:15 |

Which should yield:

2014/03/03 8:30 - start
2014/03/03 13:15 - end
5.45 - hours_difference

But of course it's only selecting

2014/03/03 8:30 - start
2014/03/03 9:00 - end
.30 - hours_difference

How can I modify my query to select the min start and max end from a given date?

Marc B

You've grouping on DAY(), which is probably incorrect. You only show data from March 2014 there, but if your DB actually has data from more months/years than just this, you'll be grouping togher 2014/03/03, 2014/02/03, 2013/02/03, etc.. (different years/months, same day-of-month)

You should be doing GROUP BY DATE(start), so you group on the full YYYY-MM-DD date value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I select rows where today's date falls in between start and end date inclusive

How do I use datetimepickers to find the difference between the entered start time and end time in hours? Visual studio c#

How to Split Hours between Min(Date) and Max(Date) into 4 Hours Slab in MySQL Server

How do I calculate % of task's completion given start date, end date, and TODAY()

How to calculate difference between max and min date for each user

If date string found to be between dates, return pay_period_start & pay_period_end. How?

Date difference between end date to start date

MySQL How to select a date record between the min and max

Pandas : Calculate difference between max and min over time period

select min(start_time) ,max(end_time) group by date

Using Joda, How do I determine if a given date is between time x and a time 3 hours earlier than x?

JPA find entities where date is between start and end date that can be null using Criteria but null value must be initialized by MAX/MIN date

How to select min date a not max date

How to get the input date between start and end date in oracle if input date not available then return max date record

How do I conditionally format a date that falls between two dates from a list of start and end dates?

How do i fill ONE cell with a range of dates between a start and end date

How do I group_by, summarize and then select both the min and max?

How do I generate a random integer between min and max in Java?

How do I validate if a start date is after an end date with Yup?

SAS Given a start & end date I need to know the dates of each 30 day period AFTER the first 35 days

Get Array Max and Min between Start and End Indexes

How to group records by hours considering start date and end date

Material calendar how to select max and min date?

How can I create range of date given the start date and end date

How to add missing data entries in an array between a given start date and end date

get monthwise working days between a given start date and end date

Mongoose query between Start Date and End Date of a given collection

Calculate days in between given start date and end date in php

Given start date and period: Find start date of current period