将请假时间汇总为订单的结束日期(尽管请假的结束日期大于订单状态的结束日期)-mysql

时间:2019-03-27 15:43:06

标签: mysql datetime sum max min

在mysql数据库中,我修改了“ leave”表:

+--------+---------+---------+-------------+---------+--------------------------
|ID_LEAVE|ID_WORKER| FNAME | LNAME | BEGIN_DATE         | END_DATE             | 
+--------+---------+---------+---------+------------+--------------------+------
| 1      |   4     | LEON  | SPEED |2019-03-22 07:00:00 |2019-03-23 15:00:00   | 
+--------+---------+---------+-------------+---------+--------------------------

“工人表”

+----------+---------+---------+
|ID_WORKER |  FNAME  | LNAME   |
+----------+---------+----------
| 4        |  LEON   |  SPEED  |
| 6        |  JACK   |  FAR    |
+----------+---------+---------+

“订单”表:

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

“订单状态”表:

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

我做了什么:

我可以在订单上总结每个工人的“订单时间”(在order_status表中),包括从假表中总结“休假时间”并总结“订单时间” +“休假时间”。我也已正确选择了彼此的工人订单(LNAME,FNAME)订单(DESC_ORDER和NUMBER_ORDER)和“ TOTAL TIME”。我在下面编写了mysql命令:

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;

但是在那之后出了点问题:

+---------+---------+---------------+------------+------------+--------------+-----------+
|  FNAME  | LNAME   |  NUMBER_ORDER | DESC_ORDER | ORDER TIME | LEAVE_TIME   | TOTAL TIME|
+---------+---------+---------------+------------+------------+--------------+-----------+
|  LEON   |  SPEED  | TEST          | TEST       | 22:30:21   |   00:00:00   |  22:30:21 |
+---------+---------+---------------+------------+------------+--------------+-----------+
|  JACK   |  FAR    | TEST          | TEST       | 21:19:18   |   00:00:00   |  21:19:18 |
+---------+---------+---------------+------------+------------+--------------+-----------+

为什么Leon Speed的“ Leave_time”设置为00:00:00,尽管他在以下时间有离开时间: 从2019-03-22 07:00:00到2019-03-23 15:00:00。

对我来说应该有所不同:

+---------+---------+---------------+------------+------------+--------------+-----------+
|  FNAME  | LNAME   |  NUMBER_ORDER | DESC_ORDER | ORDER TIME | LEAVE_TIME   | TOTAL TIME|
+---------+---------+---------------+------------+------------+--------------+-----------+
|  LEON   |  SPEED  | TEST          | TEST       | 22:30:21   |   08:00:00   |  30:30:21 |
+---------+---------+---------------+------------+------------+--------------+-----------+
|  JACK   |  FAR    | TEST          | TEST       | 21:19:18   |   00:00:00   |  21:19:18 |
+---------+---------+---------------+------------+------------+--------------+-----------+ 

因为订单于22-03-2019结束(尽管他已从22-03-2019离开至23-03-2019)。 与下面的代码行有关吗?

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

我需要如何处理的帮助。我不是一个懒惰的人,但我也不是Mysql专家(因​​为我正在训练3个星期)。有人可以提供建议或想法吗?预先感谢您的任何建议。

0 个答案:

没有答案