By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table photo_time (id int, admin_id int, appointment_id int, timestamp timestamp);
insert into photo_time values
('1', '10', '1', '2025-03-01 08:00:00'),
('2', '10', '1', '2025-03-01 09:00:00'),
('3', '10', '2', '2025-04-01 08:00:00'),
('4', '10', '2', '2025-04-01 09:00:00'),
('5', '20', '1', '2025-05-01 08:00:00'),
('6', '20', '1', '2025-05-01 09:00:00'),
('7', '20', '2', '2025-06-01 08:00:00'),
('8', '20', '2', '2025-06-01 09:00:00');
SELECT admin_id, sum(photo_time)
FROM
(
SELECT admin_id, appointment_id,
min(timestamp),
max(timestamp),
max(unix_timestamp(timestamp))-min(unix_timestamp(timestamp)) as photo_time,
count(id) as photo_count
FROM photo_time
GROUP BY admin_id,appointment_id
) by_appointment
GROUP BY admin_id;
Records: 8 Duplicates: 0 Warnings: 0
admin_id | sum(photo_time) |
---|---|
10 | 7200 |
20 | 7200 |