В базе данных MySQL я создал таблицу "Болезнь":
+--------+---------+---------+-------------+---------+-------------------------------+---------------+
| Id_SICK|ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE | SICKNESS_TIME |
+--------+---------+---------+---------+------------+--------------------+-----------+---------------+
| 4 | 26 | ANDREW | WORM |2019-03-19 07:00:00 |2019-03-19 15:00:00 | 8:00:00 |
+--------+---------+---------+----------------------+--------------------+-----------+---------------+
| 5 | 25 | ADAM | GAX |2019-03-21 07:00:00 |2019-03-21 15:00:00 | 8:00:00 |
+--------+---------+---------+----------------------+--------------------------------+---------------+
, стол "Каникулы":
+-------------+---------+---------+-------------+---------+-------------------------------+---------------+
| ID_VACATION |ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE | VACATION_TIME |
+-------------+---------+---------+---------+------------+--------------------+-----------+---------------+
| 1 | 25 | ADAM | GAX |2019-03-19 07:00:00 |2019-03-19 15:00:00 | 8:00:00 |
+-------------+---------+---------+----------------------+--------------------+-----------+---------------+
| 2 | 26 | ANDREW | WORM |2019-03-21 07:00:00 |2019-03-21 15:00:00 | 8:00:00 |
+-------------+---------+---------+----------------------+--------------------------------+---------------+
Таблица «Рабочие»:
+----------+---------+---------+
|ID_WORKER | FNAME | LNAME |
+----------+---------+----------
| 25 | ADAM | GAX |
+----------+---------+----------
| 26 | ANDREW | WORM |
+----------+---------+---------+
Таблица «Заказы»:
+----------+--------------+---------------+
|ID_ORDER | DESC_ORDER | NUMBER_ORDER |
+----------+--------------+---------------+
| 20 | TEST | TEST |
+----------+--------------+---------------+
Таблица "Order_status":
+----------+---------+---------+---------------------+-------------------+------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE | END_DATE | ORDER_DONE |
+----------+---------+---------+----------+------------+---------+--------------------+
| 47 | 25 | 20 |2019-03-18 06:50:35 |2019-03-18 15:21:32| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 48 | 25 | 20 |2019-03-20 06:44:12 |2019-03-20 15:11:23| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 50 | 25 | 20 |2019-03-22 06:50:20 |2019-03-22 12:22:33| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 51 | 26 | 20 |2019-03-18 06:45:11 |2019-03-18 15:14:45| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 52 | 26 | 20 |2019-03-20 06:50:22 |2019-03-20 15:10:32| NO |
+----------+---------+---------+------------+---------+-------------------+-----------+
| 53 | 25 | 20 |2019-03-22 06:54:11 |2019-03-22 11:23:45| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+
Что я сделал:
Я могу суммировать «общее время» рабочих друг друга (в таблице order_status) по заказу, включая суммирующее «время болезни» из таблицы болезни и «время отпуска» из таблицы отпуска. Я правильно выбрал рабочие заказы (LNAME, FNAME) (DESC_ORDER и NUMBER_ORDER) и "ВСЕГО ВРЕМЕНИ" для заказов друг друга. Я написал команду mysql ниже:
SELECT workers.fname,
workers.lname,
order_statusAgg.number_order,
order_statusAgg.desc_order,
SEC_TO_TIME(SUM(order_statusAgg.ttime)) AS 'TOTAL TIME',
SEC_TO_TIME(SUM(sicknessAgg.stime)) AS 'SICKNESS TIME',
SEC_TO_TIME(SUM(vacationAgg.vtime)) AS 'VACATION TIME'
FROM workers
INNER JOIN (
SELECT sickness.id_worker, SUM((Time_to_sec(sickness.end_date) -
Time_to_sec(sickness.begin_date))) AS stime
FROM sickness
GROUP BY sickness.id_worker
) sicknessAgg
ON sicknessAgg.id_worker = workers.id_worker
INNER JOIN (
SELECT order_status.id_worker, orders.number_order, orders.desc_order, SUM((Time_to_sec(order_status.end_date) -
Time_to_sec(order_status.begin_date))) AS ttime
FROM order_status
INNER JOIN orders
ON orders.id_order = order_status.id_order
GROUP BY order_status.id_worker
) order_statusAgg
ON workers.id_worker = order_statusAgg.id_worker
INNER JOIN ( SELECT vacation.id_worker, SUM((Time_to_sec(vacation.end_date) - Time_to_sec(vacation.begin_date))) AS vtime
FROM vacation
GROUP BY vacation.id_worker) vacationAgg
ON vacationAgg.id_worker = workers.id_worker
WHERE order_statusAgg.number_order LIKE 'TEST'
GROUP BY workers.id_worker
Тогда результат:
+---------+---------+---------------+------------+------------+--------------+--------------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | TOTAL TIME | SICKNESS_TIME| VACATION_TIME|
+---------+---------+---------------+------------+------------+--------------+--------------+
| ADAM | GAX | TEST | TEST | 22:30:21 | 8:00:00 | 8:00:00 |
+---------+---------+---------------+------------+------------+--------------+--------------+
| ANDREW | WORM | TEST | TEST | 21:19:18 | 8:00:00 | 8:00:00 |
+---------+---------+---------------+------------+------------+--------------+--------------+
Что я хотел бы сделать дальше:
Я пытаюсь обобщить их все, я имею в виду: ВСЕГО ВРЕМЕНИ + SICKNESS_TIME + VACATION_TIME. Например: где ID_WORKER = 25
Vacaton_time = 8:00:00
Sick_time = 8:00:00
Total_time = 22:30:21
SUM_TIME = vacation_time + sick_time + Total_time
SUM_TIME = 8:00:00 + 8:00:00 + 22:30:21 = 38:30:21
Я пытался добавить в верхний запрос:
SEC_TO_TIME(SUM(SEC_TO_TIME(SUM(order_statusAgg.ttime) + SUM(sicknessAgg.stime) + SUM(vacationAgg.vtime)) AS 'SUM_TIME';
Но я знаю, что это плохая идея. Так что я понятия не имею, как это решить.
У кого-нибудь есть идеи, как это решить? Есть идеи. Большое спасибо за любую помощь!