Question

How sumarize leave time to max date of order - mysql

  • 27 March 2019
  • 0 replies
  • 21 views

In this case i have created leave table:


+--------+---------+---------+-------------+---------+--------------------------
|ID_LEAVE|ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE |
+--------+---------+---------+---------+------------+--------------------+------
| 1 | 1 | ALAN | MAX |2019-03-22 07:00:00 |2019-03-23 15:00:00 |
| 2 | 1 | ALAN | MAX |2019-03-21 07:00:00 |2019-03-21 15:00:00 |
+--------+---------+---------+----------------------+---------------------------

“Workers Table”


+----------+---------+---------+
|ID_WORKER | FNAME | LNAME |
+----------+---------+----------
| 1 | ALAN | MAX |
| 2 | MARK | DARK |
+----------+---------+---------+

“Orders” Table:


+----------+--------------+---------------+
|ID_ORDER | DESC_ORDER | NUMBER_ORDER |
+----------+--------------+---------------+
| 20 | TEST | TEST |
+----------+--------------+---------------+

“Order_status” Table:


+----------+---------+---------+---------------------+-------------------+------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE | END_DATE | ORDER_DONE |
+----------+---------+---------+----------+------------+---------+--------------------+
| 30 | 1 | 20 |2019-03-18 06:50:35 |2019-03-18 15:21:32| NO |
| 31 | 1 | 20 |2019-03-20 06:44:12 |2019-03-20 15:11:23| NO |
| 32 | 1 | 20 |2019-03-22 06:50:20 |2019-03-22 12:22:33| YES |
| 33 | 2 | 20 |2019-03-18 06:45:11 |2019-03-18 15:14:45| NO |
| 34 | 2 | 20 |2019-03-20 06:50:22 |2019-03-20 15:10:32| NO |
| 35 | 2 | 20 |2019-03-22 06:54:11 |2019-03-22 11:23:45| YES |
+----------+---------+---------+------------+---------+-------------------+-----------+

What i’ve done:


I can to sumarize “total time” of each other workers (in order_status table) on the order including with sumarizing “leave time” from Leave table and sumarizing “Order time” + “leave time”. I have selected workers (LNAME, FNAME) orders (DESC_ORDER and NUMBER_ORDER) and “TOTAL TIME” on order from each other workers correctly too. I wrote the mysql command in below:


SELECT workers.FNAME, 
workers.LNAME,
order_statusAgg.NUMBER_ORDER,
order_statusAgg.DESC_ORDER,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'ORDER TIME',
IFNULL(SEC_TO_TIME(SUM(leaveAgg.ltime)),'00:00:00') AS 'LEAVE TIME',
ADDTIME(SEC_TO_TIME(SUM(order_statusAgg.stime)), IFNULL(SEC_TO_TIME(SUM(leaveAgg.ltime)), '00:00:00')) AS 'TOTAL TIME'
FROM workers
LEFT JOIN (SELECT leave.ID_WORKER, SUM((datediff(leave.BEGIN_DATE, leave.END_DATE) + 1) * (time_to_sec(time(leave.END_DATE)) - time_to_sec(time(leave.BEGIN_DATE)))) AS ltime FROM leave
LEFT JOIN
(SELECT ID_WORKER, MIN(BEGIN_DATE) AS 'MIN_BEGIN_DATE', MAX(END_DATE) AS 'MAX_END_DATE'
FROM order_status GROUP BY ID_WORKER) ordstat ON
leave.ID_WORKER = ordstat.ID_WORKER
WHERE leave.END_DATE <= MAX_END_DATE AND leave.BEGIN_DATE >= MIN_BEGIN_DATE GROUP BY leave.ID_WORKER) leaveAgg
ON leaveAgg.ID_WORKER = workers.ID_WORKER
LEFT 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 stime
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
WHERE order_statusAgg.NUMBER_ORDER LIKE 'TEST'
GROUP BY workers.ID_WORKER;

then i get:


+---------+---------+---------------+------------+------------+--------------+-----------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME | LEAVE_TIME | TOTAL TIME|
+---------+---------+---------------+------------+------------+--------------+-----------+
| ALAN | MARK | TEST | TEST | 22:30:21 | 08:00:00 | 30:30:21 |
+---------+---------+---------------+------------+------------+--------------+-----------+
| MARK | GREEN | TEST | TEST | 21:19:18 | 00:00:00 | 21:19:18 |
+---------+---------+---------------+------------+------------+--------------+-----------+

ok. But PLEASE TAKE A LOOK: I added leave to Allan Max:


2019-03-19 07:00:00 |2019-03-23 15:00:00
2019-03-21 07:00:00 |2019-03-21 15:00:00

And get’s only 8:00:00 of leave. Because this order ended on 22-03-2019. I’d like to get (despite of 2019-03-19 07:00:00 |2019-03-23 15:00:00 in Allan Max leave) the result:


+---------+---------+---------------+------------+------------+--------------+-----------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME | LEAVE_TIME | TOTAL TIME|
+---------+---------+---------------+------------+------------+--------------+-----------+
| ALAN | MARK | TEST | TEST | 22:30:21 | 16:00:00 | 38:30:21 |
+---------+---------+---------------+------------+------------+--------------+-----------+
| MARK | GREEN | TEST | TEST | 21:19:18 | 00:00:00 | 21:19:18 |
+---------+---------+---------------+------------+------------+--------------+-----------+

2019-03-22 07:00:00 |2019-03-23 15:00:00 (to the end of order 2019-03-22) = 8:00:00 (maybe) 2019-03-21 07:00:00 |2019-03-21 15:00:00 (to the end of order 2019-03-22) = 8:00:00


So in LEAVE_TIME should be 8:00:00 + 8:00:00 = 16:00:00


It’s that possible to change it? Can someone help me how to do it? thank you in advance for any help or advice.


0 replies

Be the first to reply!

Reply