how do i get the latest datetime from multiple same dates in mysql?
SELECT start_time FROM times WHERE start_time BETWEEN '2013-01-27' AND '2013-02-02' ORDER BY start_time
this outputs:
2013-01-27 00:00:00
2013-01-28 09:00:00
2013-01-29 00:00:00
2013-01-30 09:00:00
2013-01-31 00:00:00
2013-02-01 09:00:00
2013-02-01 21:00:00
2013-02-02 00:00:00
i want all this to output except i want the latest datetime for 2013-02-01
so it would output like this:
2013-01-27 00:00:00
2013-01-28 09:00:00
2013-01-29 00:00:00
2013-01-30 09:00:00
2013-01-31 00:00:00
2013-02-01 21:00:00 <<<<<<<<
2013-02-02 00:00:00
SELECT MAX(start_time)
FROM times
WHERE start_time BETWEEN '2013-01-27 00:00:00' AND '2013-02-02 23:59:59'
GROUP BY DATE(start_time)
ORDER BY start_time
SQLFiddle Demo
Related
I'm looking for a way to count the hours worked between a given time range.
For example to count from the MySQL data below the hours worked between 22:00 and 06:00.
Using date_start 2022-04-01 21:00:00 and date_end 2022-04-02 08:00:00 the user worked 11 hours total and 8 night hours.
Of course the data could also be something like 2022-04-01 05:00:00 and 2022-04-01 16:00:00 which will then need to output 2 night hours or 2022-04-01 18:00:00and 2022-04-02 03:00:00 which outputs 5 night hours.
MySQL table:
CREATE TABLE `tasks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date_start` datetime DEFAULT NULL,
`date_end` datetime DEFAULT NULL,
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tasks` (`date_start`,`date_end`) VALUES
('2022-04-01 04:00:00', '2022-04-01 16:00:00'), # 2:00 nighthours
('2022-04-02 05:00:00', '2022-04-02 23:30:00'), # 2:30 nighthours
('2022-04-03 06:00:00', '2022-04-03 18:00:00'), # 0:00 nighthours
('2022-04-04 12:00:00', '2022-04-05 00:00:00'), # 2:00 nighthours
('2022-04-05 19:00:00', '2022-04-06 07:00:00'); # 8:00 nighthours
Current MySQL:
# 21600 = 06:00 hours
# 79200 = 22:00 hours
SELECT t.date_start, t.date_end, DATE_FORMAT(TIMEDIFF(
(CASE WHEN DATE(t.date_start) != DATE(t.date_end) AND TIME_TO_SEC(t.date_end) > 21600 THEN DATE_FORMAT(t.date_end, '%Y-%m-%d 06:%i:%s')
WHEN TIME_TO_SEC(t.date_start) < 21600 THEN DATE_FORMAT(t.date_start, '%Y-%m-%d 06:%i:%s')
ELSE t.date_end END),
(CASE WHEN DATE(t.date_start) != DATE(t.date_end) AND TIME_TO_SEC(t.date_start) < 79200 THEN DATE_FORMAT(t.date_start, '%Y-%m-%d 22:%i:%s')
WHEN TIME_TO_SEC(t.date_end) > 79200 THEN DATE_FORMAT(t.date_start, '%Y-%m-%d 22:%i:%s')
WHEN DATE(t.date_start) = DATE(t.date_end) AND TIME_TO_SEC(t.date_end) <= 79200 AND TIME_TO_SEC(t.date_start) >= 21600 THEN t.date_end
ELSE t.date_start END)
), '%H:%i') AS night_time FROM tasks t;
Currently I still have a problem in my current MySQL when the start_date and end_date both start on the same day and both have night hours. for example 2022-04-02 05:00:00 and 2022-04-02 23:30:00 which has 01:00 night hour in start_date and 1:30 hour in end_date (total night: 02:30 hours)
I am not sure if my current MySQL is the best/fastest way to achieve my goal.
Calculating Time Overlaps
You can calculate the amount of time two date ranges overlap using:
MIN( EndDate1, EndDate2 ) - MAX( StartDate1, StartDate2 )
For example if the date ranges are:
Date_Start
Date_End
Night_Shift_Start
Night_Shift_End
2022-04-01 21:00:00
2022-04-02 08:00:00
2022-04-01 22:00:00 **
2022-04-02 06:00:00 **
The result would be 8 hours:
Min( EndDate ) - Max( StartDate )
.... As Unix Timestamps
Time Overlap
2022-04-02 06:00:00 (minus) 2022-04-01 22:00:00
1648875600 - 1648846800 = 28800 seconds
08:00:00 hours
Checking for Multiple Overlaps
Since technically a single shift could have both started and ended during "night hours" (22:00 to 06:00) you need check for overlaps on both sides.
Date_Start
Date_End
Night Hours
...
2022-04-05 05:00:00
2022-04-05 23:30:00
2.5 hours
(1 hour) : 2022-04-05 05:00 to 2022-04-05 06:00 (1.5 hours) : 2022-04-05 22:00 to 2022-04-05 23:30
One approach is using the base start/end times to calculate the previous and upcoming "night hour" periods:
SELECT *
, TIMESTAMP(DATE(date_start) - INTERVAL 1 DAY, '22:00:00') AS current_start
, TIMESTAMP(DATE(date_start), '06:00:00') AS current_end
, TIMESTAMP(DATE(date_start), '22:00:00') AS next_start
, TIMESTAMP(DATE(date_start) + INTERVAL 1 DAY, '06:00:00') AS next_end
FROM tasks
Results:
id
date_start
date_end
current_start
current_end
next_start
next_end
1
2022-04-01 04:00:00
2022-04-01 16:00:00
2022-03-31 22:00:00
2022-04-01 06:00:00
2022-04-01 22:00:00
2022-04-02 06:00:00
2
2022-04-02 05:00:00
2022-04-02 23:30:00
2022-04-01 22:00:00
2022-04-02 06:00:00
2022-04-02 22:00:00
2022-04-03 06:00:00
3
2022-04-03 06:00:00
2022-04-03 18:00:00
2022-04-02 22:00:00
2022-04-03 06:00:00
2022-04-03 22:00:00
2022-04-04 06:00:00
4
2022-04-04 12:00:00
2022-04-05 00:00:00
2022-04-03 22:00:00
2022-04-04 06:00:00
2022-04-04 22:00:00
2022-04-05 06:00:00
5
2022-04-05 19:00:00
2022-04-06 07:00:00
2022-04-04 22:00:00
2022-04-05 06:00:00
2022-04-05 22:00:00
2022-04-06 06:00:00
6
2022-04-01 04:00:00
2022-04-01 16:00:00
2022-03-31 22:00:00
2022-04-01 06:00:00
2022-04-01 22:00:00
2022-04-02 06:00:00
7
2022-04-05 19:00:00
2022-04-06 07:00:00
2022-04-04 22:00:00
2022-04-05 06:00:00
2022-04-05 22:00:00
2022-04-06 06:00:00
8
2022-04-05 05:00:00
2022-04-05 23:30:00
2022-04-04 22:00:00
2022-04-05 06:00:00
2022-04-05 22:00:00
2022-04-06 06:00:00
Total Overlap Time
Once you have the "night hour" ranges, calculate the overlapping time on both sides and add them together to get the total time worked during "night hours"
SELECT id
, date_start
, date_end
, SEC_TO_TIME(
GREATEST(0, start_overlap__in_seconds) -- ignore negative time, which means no overlap
+ GREATEST(0, end_overlap_in_seconds)
) AS time_overall
FROM (
SELECT *
, UNIX_TIMESTAMP(LEAST(date_end, current_end))
- UNIX_TIMESTAMP(GREATEST(date_start, current_start))
AS start_overlap__in_seconds
, UNIX_TIMESTAMP(LEAST(date_end,next_end))
- UNIX_TIMESTAMP(GREATEST(date_start,next_start))
AS end_overlap_in_seconds
FROM (
SELECT *
, TIMESTAMP(DATE(date_start) - INTERVAL 1 DAY, '22:00:00') AS current_start
, TIMESTAMP(DATE(date_start), '06:00:00') AS current_end
, TIMESTAMP(DATE(date_start), '22:00:00') AS next_start
, TIMESTAMP(DATE(date_start) + INTERVAL 1 DAY, '06:00:00') AS next_end
FROM tasks
) tmp
) t
Final Results:
id
date_start
date_end
time_overall
1
2022-04-01 04:00:00
2022-04-01 16:00:00
02:00:00
2
2022-04-02 05:00:00
2022-04-02 23:30:00
02:30:00
3
2022-04-03 06:00:00
2022-04-03 18:00:00
00:00:00
4
2022-04-04 12:00:00
2022-04-05 00:00:00
02:00:00
5
2022-04-05 19:00:00
2022-04-06 07:00:00
08:00:00
6
2022-04-01 04:00:00
2022-04-01 16:00:00
02:00:00
7
2022-04-05 19:00:00
2022-04-06 07:00:00
08:00:00
8
2022-04-05 05:00:00
2022-04-05 23:30:00
02:30:00
db<>fiddle here
I have a sample database.
Name CheckIn CheckOut
Jake 2017-08-02 00:00:00 2017-08-05 00:00:00
Rowan 2017-08-07 00:00:00 2017-08-11 00:00:00
Xander 2017-08-08 00:00:00 2017-08-10 00:00:00
Anna 2017-08-09 00:00:00 2017-08-15 00:00:00
Nat 2017-08-11 00:00:00 2017-08-14 00:00:00
For example user search the date of 2017-08-08 to 2017-08-10. User want to search the all data that who has the date of 8, 9 and 10. Ex. Rowan choose the date of checkin in 7 and checkout in 10, So rowan had the the date of 7, 8, 9 10 and 11. This is the output that i want.
Name CheckIn CheckOut
Rowan 2017-08-07 00:00:00 2017-08-11 00:00:00
Anna 2017-08-09 00:00:00 2017-08-15 00:00:00
Xander 2017-08-08 00:00:00 2017-08-10 00:00:00
I really dont know the logic of reservation searching help me. Thanks guys.
If you want to check for overlapping intervals, then this is the way:
SELECT Name, CheckIn, CheckOut
FROM mytable
WHERE CheckIn <= '2017-08-10' AND CheckOut >= '2017-08-08';
Demo here
To better understand this you can draw a sketch depicting the search interval [2017-08-08, 2017-08-10] against the reservation interval [CheckIn, CheckOut]:
If:
CheckOut < '2017-08-08' then the reservation interval comes before the search interval
CheckIn > '2017-08-10` then the reservation interval comes after the search interval
Hence none of the above conditions must hold if we want the intervals to overlap. So we end up with the following predicates:
`CheckOut` >= '2017-08-08' AND `CheckIn` <= '2017-08-10`
try this:
SELECT * FROM test.TableName WHERE
(DATE(CheckIn) BETWEEN DATE('2017-08-08') AND DATE('2017-08-10') )
OR
(DATE(CheckOut) BETWEEN DATE('2017-08-08') AND DATE('2017-08-10') )
OR
(DATE('2017-08-08') BETWEEN DATE(CheckIn) AND DATE(CheckOut))
OR
(DATE('2017-08-10') BETWEEN DATE(CheckIn) AND DATE(CheckOut));
You need a between condition for start and end date in where clause for check_in and check_out columns as below.
SELECT *
FROM table1
WHERE check_in BETWEEN '2017-08-07' AND '2017-08-10'
OR check_out BETWEEN '2017-08-07' AND '2017-08-10'
Result
name check_in check_out
-------------------------------------------------
Rowan 07.08.2017 00:00:00 11.08.2017 00:00:00
Xander 08.08.2017 00:00:00 10.08.2017 00:00:00
Anna 09.08.2017 00:00:00 15.08.2017 00:00:00
You can check the demo here
I have table like this :
EmployeeID date1 date2 time1 time2 totaltime
12345 2015-09-15 2015-09-15 10:00:00 15:00:00 5
12345 2015-09-15 2015-09-15 08:00:00 09:00:00 1
12345 2015-09-17 2015-09-17 08:00:00 09:00:00 1
12345 2015-09-19 2015-09-19 10:00:00 15:00:00 5
78901 2015-09-30 2015-09-30 10:00:00 15:00:00 5
The problem is i want to distinct the EmployeeID and the date1 coloumn and sum the total time onl for one employeeID who has same date (based on date1). You can see on my example result below :
EmployeeID date1 date2 time1 time2 totaltime
12345 2015-09-15 2015-09-15 10:00:00 15:00:00 5
12345 2015-09-15 2015-09-15 08:00:00 09:00:00 1
so, the result is 6 (5+1).
I have tried to use this syntax :
SELECT DISTINCT (EmployeeID, date1) as totaldate
but nothing happened.
Where is the problem?
And I'm sorry for my bad english.
try this
SELECT EmployeeID, date1, SUM(totaltime) as totaldate
FROM your_table
GROUP BY EmployeeID, date1
I have a request table..
user_id no:of_mach time_start req_time
11 3 2012-12-12 09:00:00 2012-12-11 09:00:00
12 4 2012-12-14 08:00:00 2012-12-14 06:00:00
13 4 2012-12-12 09:00:00 2012-12-12 02:00:00
14 2 2013-12-12 07:00:00 2012-12-12 03:00:00
15 2 2012 12-14 08:00:00 2012-12-14 05:00:00
From the above table, I need to get the req_time of the users who has requested for the same time_start.
The duplicate time_start are
2012-12-12 09:00:00 by user_id 11,13.
2012-12-14 08:00:00 by user_id 12,15.
Now, each of theirs request time is different..
I want a query so that it will get me the result as:-
req_time of user requested for the time_start 2012-12-12 09:00:00 are:-
2012-12-11 09:00:00
2012-12-12 02:00:00
req_time of user requested for the time_start 2012-12-14 08:00:00 are:-
2012-12-14 06:00:00
2012-12-14 05:00:00
I have used a query:-
SELECT req_time FROM user_req WHERE user_id IN (SELECT o.user_id FROM user_req o INNER JOIN ( SELECT starttime, COUNT( * ) AS dupeCount FROM user_req GROUP BY starttime HAVING COUNT( * ) >1)oc ON o.starttime = oc.starttime) ORDER BY req_time ASC;
And this prints all the req_time together for all the duplicate time_start values..
The output will be :-
2012-12-11 09:00:00
2012-12-12 02:00:00
2012-12-14 06:00:00
2012-12-14 05:00:00
Can I have a query that help me to group this req_time based on each duplicate time_start which I have explained above.
Then I can call it in java and use it for my program..
Please help me..
Try this:
select * from user_req where time_start in
(select time_start
from user_req
group by time_start
having count(time_start) > 1)
order by time_start, req_time
This will return records from the table with multiple counts of same time_start, ordered by the start_time and req_time. You can choose to show only those 2 columns if you want by replacing the select * with appropriate column names.
(
SELECT *
FROM (
SELECT d
FROM myTable
WHERE id = "4h"
AND d < "2011-12-08 12:00:00"
ORDER BY d DESC
LIMIT 10
)tmp
ORDER BY d ASC
)
UNION (
SELECT d
FROM myTable
WHERE id = "4h"
AND d >= "2011-12-08 12:00:00"
ORDER BY d ASC
LIMIT 10
)
I'm trying to get the 10 results before and after a particular ID by using two SELECT statements and a UNION. The first SELECT uses ORDER BY DESC to get the 10 preceding and then I attempt to envelope that in a second ORDER BY ASC to get all the results in ASC order but for some reason it does not work.
Here is what I get currently for a result:
d
2011-12-08 08:00:00
2011-12-08 04:00:00
2011-12-08 00:00:00
2011-12-07 20:00:00
2011-12-07 16:00:00
2011-12-07 12:00:00
2011-12-07 08:00:00
2011-12-07 04:00:00
2011-12-07 00:00:00
2011-12-06 20:00:00 <- These top 10 results should ASC!
2011-12-08 12:00:00
2011-12-08 16:00:00
2011-12-08 20:00:00
2011-12-09 00:00:00
2011-12-09 04:00:00
2011-12-09 08:00:00
2011-12-09 12:00:00
2011-12-09 16:00:00
2011-12-09 20:00:00
2011-12-11 20:00:00
And here is what I want:
d
2011-12-06 20:00:00
2011-12-07 00:00:00
2011-12-07 04:00:00
2011-12-07 08:00:00
2011-12-07 12:00:00
2011-12-07 16:00:00
2011-12-07 20:00:00
2011-12-08 00:00:00
2011-12-08 04:00:00
2011-12-08 08:00:00
2011-12-08 12:00:00
2011-12-08 16:00:00
2011-12-08 20:00:00
2011-12-09 00:00:00
2011-12-09 04:00:00
2011-12-09 08:00:00
2011-12-09 12:00:00
2011-12-09 16:00:00
2011-12-09 20:00:00
2011-12-11 20:00:00
(
SELECT d
FROM myTable
WHERE id = '4h' AND d < '2011-12-08 12:00:00'
ORDER BY d DESC
LIMIT 10
) UNION ALL (
SELECT d
FROM myTable
WHERE id = '4h' AND d >= '2011-12-08 12:00:00'
ORDER BY d ASC
LIMIT 10
)
ORDER BY d ASC