如何将总和转换为时间 - mysql
How to convert from sum to time - mysql
我在 mysql 数据库中创建了 "leave" table:
+--------+---------+---------+-------------+---------+-------------------------------+
|ID_LEAVE|ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE |
+--------+---------+---------+---------+------------+--------------------+-----------
| 1 | 1 | ALAN | MAX |2019-03-19 07:00:00 |2019-03-20 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 |
+----------+---------+---------+------------+---------+-------------------+-----------+
我做了什么:
我可以汇总 "total time" 的其他工人(在 order_status table 中),包括汇总离开 table 的 "leave time"。我也正确地选择了工人(LNAME、FNAME)订单(DESC_ORDER 和 NUMBER_ORDER)和 "TOTAL TIME" 订单。我在下面写了 mysql 命令:
SELECT workers.fname,
workers.lname,
order_statusAgg.number_order,
workers.id_worker,
order_statusAgg.desc_order,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'TOTAL TIME',
IFNULL(SEC_TO_TIME(SUM(leaveAgg.vtime)),'00:00:00') AS 'LEAVE TIME'
FROM workers
LEFT JOIN (
SELECT leave.id_worker, time_format(SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time(leave.end_date) - time(leave.begin_date))), '%H:%i:%s') AS vtime
FROM leave
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 | TOTAL TIME | LEAVE_TIME |
+---------+---------+---------------+------------+------------+--------------+
| ALAN | MARK | TEST | TEST | 22:30:21 |00:00:24.000000|
+---------+---------+---------------+------------+------------+--------------+
| MARK | GREEN | TEST | TEST | 21:19:18 | 00:00:00 |
+---------+---------+---------------+------------+------------+--------------+
但是关于休假时间是不正确的,因为当我写那个命令时:
SELECT leave.id_worker,
time_format(SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time(leave.end_date) - time(leave.begin_date))), '%H:%i:%s') AS vtime
FROM leave
GROUP BY leave.id_worker
+----------+---------+
|ID_WORKER | vtime |
+----------+---------+
| 1 | 24:00:00|
+----------+---------+
我试过这样做:
1) IFNULL(SUM(leaveAgg.vtime),'00:00:00') AS 'Leave time'
2) IFNULL(time_format(SUM(leaveAgg.vtime),'%H:%i:%s'),'00:00:00') AS 'Leave time'
但是其中 none 个没有用。
所以在这种情况下我需要将计数转换为日期时间
00:00:24.000000 => 24:00:00
我一直在寻找想法,但我还没有找到成功的解决方案。有任何想法吗?有人可以告诉我应该做什么或可以写什么样的查询吗?感谢您的帮助!
SELECT workers.fname,
workers.lname,
order_statusAgg.number_order,
workers.id_worker,
order_statusAgg.desc_order,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'TOTAL TIME',
IFNULL(SEC_TO_TIME(SUM(leaveAgg.vtime)),'00:00:00') AS 'LEAVE TIME'
FROM workers
LEFT JOIN (
SELECT leave.id_worker, SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time_to_sec(time(leave.end_date)) - time_to_sec(time(leave.begin_date)))) AS vtime
FROM leave
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;
我没有做太多,我只是改变了这部分:
SELECT leave.id_worker, time_format(SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time(leave.end_date) - time(leave.begin_date))), '%H:%i:%s') AS vtime
到
SELECT leave.id_worker, SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time_to_sec(time(leave.end_date)) - time_to_sec(time(leave.begin_date)))) AS vtime
在你的 leaveAgg
.
我在 mysql 数据库中创建了 "leave" table:
+--------+---------+---------+-------------+---------+-------------------------------+
|ID_LEAVE|ID_WORKER| FNAME | LNAME | BEGIN_DATE | END_DATE |
+--------+---------+---------+---------+------------+--------------------+-----------
| 1 | 1 | ALAN | MAX |2019-03-19 07:00:00 |2019-03-20 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 |
+----------+---------+---------+------------+---------+-------------------+-----------+
我做了什么:
我可以汇总 "total time" 的其他工人(在 order_status table 中),包括汇总离开 table 的 "leave time"。我也正确地选择了工人(LNAME、FNAME)订单(DESC_ORDER 和 NUMBER_ORDER)和 "TOTAL TIME" 订单。我在下面写了 mysql 命令:
SELECT workers.fname,
workers.lname,
order_statusAgg.number_order,
workers.id_worker,
order_statusAgg.desc_order,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'TOTAL TIME',
IFNULL(SEC_TO_TIME(SUM(leaveAgg.vtime)),'00:00:00') AS 'LEAVE TIME'
FROM workers
LEFT JOIN (
SELECT leave.id_worker, time_format(SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time(leave.end_date) - time(leave.begin_date))), '%H:%i:%s') AS vtime
FROM leave
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 | TOTAL TIME | LEAVE_TIME |
+---------+---------+---------------+------------+------------+--------------+
| ALAN | MARK | TEST | TEST | 22:30:21 |00:00:24.000000|
+---------+---------+---------------+------------+------------+--------------+
| MARK | GREEN | TEST | TEST | 21:19:18 | 00:00:00 |
+---------+---------+---------------+------------+------------+--------------+
但是关于休假时间是不正确的,因为当我写那个命令时:
SELECT leave.id_worker,
time_format(SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time(leave.end_date) - time(leave.begin_date))), '%H:%i:%s') AS vtime
FROM leave
GROUP BY leave.id_worker
+----------+---------+
|ID_WORKER | vtime |
+----------+---------+
| 1 | 24:00:00|
+----------+---------+
我试过这样做:
1) IFNULL(SUM(leaveAgg.vtime),'00:00:00') AS 'Leave time'
2) IFNULL(time_format(SUM(leaveAgg.vtime),'%H:%i:%s'),'00:00:00') AS 'Leave time'
但是其中 none 个没有用。
所以在这种情况下我需要将计数转换为日期时间
00:00:24.000000 => 24:00:00
我一直在寻找想法,但我还没有找到成功的解决方案。有任何想法吗?有人可以告诉我应该做什么或可以写什么样的查询吗?感谢您的帮助!
SELECT workers.fname,
workers.lname,
order_statusAgg.number_order,
workers.id_worker,
order_statusAgg.desc_order,
SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'TOTAL TIME',
IFNULL(SEC_TO_TIME(SUM(leaveAgg.vtime)),'00:00:00') AS 'LEAVE TIME'
FROM workers
LEFT JOIN (
SELECT leave.id_worker, SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time_to_sec(time(leave.end_date)) - time_to_sec(time(leave.begin_date)))) AS vtime
FROM leave
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;
我没有做太多,我只是改变了这部分:
SELECT leave.id_worker, time_format(SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time(leave.end_date) - time(leave.begin_date))), '%H:%i:%s') AS vtime
到
SELECT leave.id_worker, SUM((datediff(leave.end_date, leave.begin_date) + 1) * (time_to_sec(time(leave.end_date)) - time_to_sec(time(leave.begin_date)))) AS vtime
在你的 leaveAgg
.