gpt4 book ai didi

mysql - 按比例正确汇总休假时间 - mysql

转载 作者:可可西里 更新时间:2023-11-01 07:43:10 27 4
gpt4 key购买 nike

在 mysql 数据库中,我创建了“离开”表:

CREATE TABLE `leave` ( 
`ID_LEAVE` int(11) NOT NULL,
`ID_WORKER` int(11) NOT NULL,
`BEGIN_DATE` datetime DEFAULT NULL,
`END_DATE` datetime DEFAULT NULL
);

INSERT INTO `leave`
(`ID_LEAVE`, `ID_WORKER`, `BEGIN_DATE`, `END_DATE`) VALUES
(3, 26, '2019-03-20 07:00:00', '2019-03-21 15:00:00'),
(4, 22, '2019-03-20 07:00:00', '2019-03-20 15:00:00');

“ worker ”表:

CREATE TABLE `workers` (
`ID_WORKER` int(11) NOT NULL,
`FNAME` varchar(20) NOT NULL,
`LNAME` varchar(20) NOT NULL
);

INSERT INTO `workers` (`ID_WORKER`, `FNAME`, `LNAME`) VALUES
(22, 'ALAN', 'FAST'),
(23, 'LEON', 'SPEED'),
(24, 'ADAM', 'GREEN'),
(25, 'DAVID', 'BUCS'),
(26, 'JACK', 'FAR'),
(27, 'ADAM', 'GAX'),
(28, 'ANDREW', 'WORM');

“订单”表:

CREATE TABLE `orders` (
`ID_ORDER` int(11) NOT NULL,
`DESC_ORDER` varchar(50) NOT NULL,
`NUMBER_ORDER` varchar(30) NOT NULL
);

INSERT INTO `orders` (`ID_ORDER`, `DESC_ORDER`, `NUMBER_ORDER`) VALUES
(19, 'TEST', 'TEST'),
(20, 'TEST2', 'TEST2'),
(21, 'TEST3', 'TEST3'),
(22, 'TEST4', 'TEST4');

“Order_status”表(抱歉,顺序不对):

CREATE TABLE `order_status` (
`ID_STATUS` int(11) NOT NULL,
`ID_ORDER` int(11) NOT NULL,
`ID_WORKER` int(11) NOT NULL,
`BEGIN_DATE` datetime NOT NULL,
`END_DATE` datetime NOT NULL,
`ORDER_DONE` varchar(50) DEFAULT NULL
) ;

INSERT INTO `order_status` (`ID_STATUS`, `ID_ORDER`, `ID_WORKER`, `BEGIN_DATE`, `END_DATE`, `ORDER_DONE`) VALUES
(30, 19, 26, '2019-03-18 06:40:21', '2019-03-18 15:22:32', 'NO'),
(31, 19, 25, '2019-03-18 06:51:23', '2019-03-18 15:11:10', 'NO'),
(32, 19, 26, '2019-03-20 06:23:32', '2019-03-20 15:33:11', 'NO'),
(33, 19, 25, '2019-03-20 06:44:42', '2019-03-20 15:16:22', 'NO'),
(34, 19, 26, '2019-03-22 06:44:32', '2019-03-22 11:44:54', 'YES'),
(35, 19, 25, '2019-03-22 06:59:22', '2019-03-22 12:33:33', 'YES'),
(42, 20, 22, '2019-03-18 06:44:23', '2019-03-18 15:11:23', 'NO'),
(43, 20, 23, '2019-03-18 06:50:45', '2019-03-18 15:15:44', 'NO'),
(44, 20, 22, '2019-03-19 06:40:20', '2019-03-19 15:23:30', 'NO'),
(45, 20, 22, '2019-03-21 06:55:45', '2019-03-21 16:03:23', 'NO'),
(46, 20, 22, '2019-03-22 06:55:45', '2019-03-22 13:23:44', 'YES'),
(47, 20, 23, '2019-03-19 06:45:23', '2019-03-19 15:33:23', 'NO'),
(48, 20, 23, '2019-03-20 06:45:23', '2019-03-20 15:22:23', 'NO'),
(49, 20, 23, '2019-03-21 06:50:33', '2019-03-21 15:33:11', 'NO'),
(50, 20, 23, '2019-03-22 06:44:23', '2019-03-22 12:22:44', 'YES'),
(51, 21, 24, '2019-03-18 06:46:55', '2019-03-18 15:23:22', 'NO'),
(52, 21, 24, '2019-03-19 06:55:33', '2019-03-19 15:23:33', 'NO'),
(53, 21, 24, '2019-03-20 06:50:59', '2019-03-20 15:44:02', 'NO'),
(54, 21, 24, '2019-03-21 06:44:33', '2019-03-21 15:23:33', 'YES'),
(55, 21, 24, '2019-03-22 06:49:33', '2019-03-22 11:22:32', 'YES'),
(56, 22, 27, '2019-03-18 06:50:12', '2019-03-18 15:22:15', 'NO'),
(57, 22, 28, '2019-03-18 06:44:12', '2019-03-18 15:33:44', 'NO'),
(58, 22, 27, '2019-03-19 06:53:22', '2019-03-19 15:22:33', 'NO'),
(59, 22, 28, '2019-03-19 06:33:22', '2019-03-19 15:15:22', 'NO'),
(60, 22, 28, '2019-03-21 06:33:22', '2019-03-21 15:01:56', 'NO'),
(61, 22, 27, '2019-03-22 06:33:22', '2019-03-22 11:05:33', 'YES'),
(62, 22, 27, '2019-03-21 06:45:22', '2019-03-21 15:33:33', 'NO'),
(63, 22, 28, '2019-03-22 06:51:33', '2019-03-22 10:35:55', 'YES'),
(64, 19, 25, '2019-03-19 06:50:32', '2019-03-19 15:33:44', 'NO'),
(65, 19, 26, '2019-03-19 06:44:50', '2019-03-19 15:22:33', 'NO'),
(66, 19, 25, '2019-03-21 06:50:50', '2019-03-21 15:33:33', 'NO'),
(67, 22, 27, '2019-03-20 06:51:32', '2019-03-20 15:20:33', 'NO');

我做了什么:

我可以总结每个其他 worker 的“订单时间”。我也正确地选择了 worker (LNAME、FNAME)订单(DESC_ORDER 和 NUMBER_ORDER)和其他 worker 订购的“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'
FROM workers
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 |
+---------+---------+---------------+------------+------------+
| DAVID | BUCS | TEST | TEST | 22:30:21 |
+---------+---------+---------------+------------+------------+
| JACK | FAR | TEST | TEST | 21:19:18 |
+---------+---------+---------------+------------+------------+

我分析了 2 天(2019 年 3 月 20 日和 2019 年 3 月 21 日)。照片如下:

20.03.2019

21.03.2019

现在我需要 mysql 查询来正确和按比例汇总离开时间以符合下面描述的模式:

pattern to proportional leave time

例如 ID_WORKER = 26 的休假时间(他有订单“TEST”)

                    '08:31:40'                                      '08:31:40'
(-------------------------------------------------) * 8:00:00 = (--------------) * 8:00:00 = 2:00:16
'08:31:40' + '08:37:00' + '08:53:03' + '08:29:01' '34:30:44'

我试过的方法:我试过在下面通过 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'
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;

但恐怕这还不够,因为它只添加了例如:

22:30:21 + 8:00:00 = 30:30:21

我想得到那个结果:

22:30:21 + 2:00:16 = 24:30:37

注意!如果某人有 2 天或更多天的假期,我不能这样做:

                    '08:31:40'                                      
(-------------------------------------------------) * 16:00:00
'08:31:40' + '08:37:00' + '08:53:03' + '08:29:01'

我必须像我解释的那个例子一样每天分析。

我知道这是一个更难的问题,我找不到任何解决方法。有人可以请我更改或添加什么吗?预先感谢您提供任何帮助或建议。

最佳答案

您的 order_status 表中似乎有一些额外的数据,因为我得到了一些额外的行。但是过滤掉这些并运行一天我得到了结果。我在电子表格中仔细检查了结果,它似乎是正确的:

(08:31:40/34:30:44) * 08:00:00 = 01:58:36.3194

无论如何,这是查询,您正在寻找 window functions解决您的问题:

SELECT *,
SEC_TO_TIME((d.ORDER_TIME / d.DAILY_TOTAL) * 28800) as 'LEAVE TIME'
FROM
(
SELECT os.*,
o.NUMBER_ORDER,
((TIME_TO_SEC(os.END_DATE) - TIME_TO_SEC(os.BEGIN_DATE))) AS 'ORDER_TIME',
(SUM(TIME_TO_SEC(os.END_DATE) - TIME_TO_SEC(os.BEGIN_DATE)) OVER (PARTITION BY CAST(os.END_DATE AS DATE) ORDER BY o.ID_ORDER ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) as 'DAILY_TOTAL'
FROM order_status os
INNER JOIN orders o ON o.ID_ORDER = os.ID_ORDER
WHERE (
CAST(os.BEGIN_DATE AS DATE) = '2019-03-20' OR
CAST(os.END_DATE AS DATE) = '2019-03-20'
)
AND NOT (
os.ID_STATUS = 32 AND
os.ID_ORDER = 19 AND
os.ID_WORKER = 26)
) d

关于mysql - 按比例正确汇总休假时间 - mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443247/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com