0

I have a list of times that I need to sum. I have two tables: workers and schedule.

CREATE TABLE schedule (
    id,
    worker_id,
    date,
    start_time,
    end_time,
    hours

SELECT w.name, w.salary, time(sum(strftime('%s', hours) - strftime('%s', '00:00:00')), 'unixepoch') AS total_hours
FROM workers w
JOIN schedule s ON s.worker_id = w.id
  WHERE  date between '2018-09-17' AND '2018-09-21'
GROUP BY w.name

The column hours contain the total of hours worked for every day. I found some other posts, but not the solution that I need.

  1. sqlite: how to add total time hh:mm:ss where column datatype is DATETIME?
  2. sqlite: sum time over 24h to HHH:MM:SS

Problem

Image schedule table This request works fine if I sum values under 24Hrs : example 10:00 + 07:00 the total will be : 17:00, but if I sum more times : 10:00 + 07:00 + 09:00 I will get :02:00.
I don't know what I am doing wrong.

The result what I am looking for :

Worker   | Salary | Total Hours
John Doe | $28.00 | 26:00
worker 1 | $30.00 | 20:15
worker 2 | $25.00 | 42:30
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Vargas
  • 11
  • 6

1 Answers1

1

Just represent the value as decimal hours, so 10:30 would be represented instead at 10.5:

SELECT w.name, w.salary,
       sum(strftime('%s', hours) - strftime('%s', '00:00:00')) / (60.0 * 60) as decimal_hours
FROM workers w JOIN
     schedule s
     ON s.worker_id = w.id
WHERE  date between '2018-09-17' AND '2018-09-21'
GROUP BY w.name;

You can format this back into a string of the form HH:MM, if you really, really want. I suggest that you keep it as decimal hours or minutes, though.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786