Sumarize leave time proportionally and correctly - mysql The Next CEO of Stack Overflowget...

Find a path from s to t using as few red nodes as possible

Gödel's incompleteness theorems - what are the religious implications?

Car headlights in a world without electricity

How to compactly explain secondary and tertiary characters without resorting to stereotypes?

Oldie but Goldie

Is the 21st century's idea of "freedom of speech" based on precedent?

How did scripture get the name bible?

Can you teleport closer to a creature you are Frightened of?

How can the PCs determine if an item is a phylactery?

What steps are necessary to read a Modern SSD in Medieval Europe?

Why doesn't Shulchan Aruch include the laws of destroying fruit trees?

Early programmable calculators with RS-232

That's an odd coin - I wonder why

Does the Idaho Potato Commission associate potato skins with healthy eating?

Read/write a pipe-delimited file line by line with some simple text manipulation

"Eavesdropping" vs "Listen in on"

Does Germany produce more waste than the US?

Can I hook these wires up to find the connection to a dead outlet?

How to show a landlord what we have in savings?

Why can't we say "I have been having a dog"?

How to find if SQL server backup is encrypted with TDE without restoring the backup

Is it correct to say moon starry nights?

What does this strange code stamp on my passport mean?

Can I cast Thunderwave and be at the center of its bottom face, but not be affected by it?



Sumarize leave time proportionally and correctly - mysql



The Next CEO of Stack Overflowget column from too many tables in mysqlMysql int vs varchar as primary key (InnoDB Storage Engine?Need to find the employees who have switched jobs atleast twiceMysql join not workingOptimizing a simple query on a large tableHow to improve query count execution with mySql replicate?MySQL query taking too longselect MAX() from MySQL view (2x INNER JOIN) is slowslow queries on indexed columns (large datasets)Why does the Select statement stalls when executed directly in sequence?












0















In mysql database i created "leave" table:



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');


"Workers" table:



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');


"Orders" table:



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" table (I'm sorry for that's not in order):



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` tinyint(1) 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');


What i've done:



I can to sumarize "Order time" of each other workers. 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'
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;


Then i get:



+---------+---------+---------------+------------+------------+
| FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME |
+---------+---------+---------------+------------+------------+
| DAVID | BUCS | TEST | TEST | 22:30:21 |
+---------+---------+---------------+------------+------------+
| JACK | FAR | TEST | TEST | 21:19:18 |
+---------+---------+---------------+------------+------------+


I've analyzed 2 days (20.03.2019 and 21.03.2019). Photos are in below:



enter image description here



enter image description here



Now i need mysql query that sumarize leave time correctly and PROPORTIONALLY to pattern described in below:



enter image description here



for example for leave time where ID_WORKER = 26 (he has order "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'


What I've tried: I've tried sum leave time by mysql query 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'
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;


But I'm afraid that isn't enough because it only adds for example:



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


I wanna get that result:



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


CAUTION! If someone get's 2 or more days of leaves i cannot do that like this:



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


I have to analyze every day like that example where i explained.



I know that's more difficult question i cannot find any clue how to solve it. Can someone please what should i change or add. Thank you in advance for any help or advice.









share







New contributor




Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    In mysql database i created "leave" table:



    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');


    "Workers" table:



    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');


    "Orders" table:



    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" table (I'm sorry for that's not in order):



    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` tinyint(1) 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');


    What i've done:



    I can to sumarize "Order time" of each other workers. 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'
    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;


    Then i get:



    +---------+---------+---------------+------------+------------+
    | FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME |
    +---------+---------+---------------+------------+------------+
    | DAVID | BUCS | TEST | TEST | 22:30:21 |
    +---------+---------+---------------+------------+------------+
    | JACK | FAR | TEST | TEST | 21:19:18 |
    +---------+---------+---------------+------------+------------+


    I've analyzed 2 days (20.03.2019 and 21.03.2019). Photos are in below:



    enter image description here



    enter image description here



    Now i need mysql query that sumarize leave time correctly and PROPORTIONALLY to pattern described in below:



    enter image description here



    for example for leave time where ID_WORKER = 26 (he has order "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'


    What I've tried: I've tried sum leave time by mysql query 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'
    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;


    But I'm afraid that isn't enough because it only adds for example:



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


    I wanna get that result:



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


    CAUTION! If someone get's 2 or more days of leaves i cannot do that like this:



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


    I have to analyze every day like that example where i explained.



    I know that's more difficult question i cannot find any clue how to solve it. Can someone please what should i change or add. Thank you in advance for any help or advice.









    share







    New contributor




    Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      In mysql database i created "leave" table:



      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');


      "Workers" table:



      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');


      "Orders" table:



      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" table (I'm sorry for that's not in order):



      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` tinyint(1) 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');


      What i've done:



      I can to sumarize "Order time" of each other workers. 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'
      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;


      Then i get:



      +---------+---------+---------------+------------+------------+
      | FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME |
      +---------+---------+---------------+------------+------------+
      | DAVID | BUCS | TEST | TEST | 22:30:21 |
      +---------+---------+---------------+------------+------------+
      | JACK | FAR | TEST | TEST | 21:19:18 |
      +---------+---------+---------------+------------+------------+


      I've analyzed 2 days (20.03.2019 and 21.03.2019). Photos are in below:



      enter image description here



      enter image description here



      Now i need mysql query that sumarize leave time correctly and PROPORTIONALLY to pattern described in below:



      enter image description here



      for example for leave time where ID_WORKER = 26 (he has order "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'


      What I've tried: I've tried sum leave time by mysql query 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'
      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;


      But I'm afraid that isn't enough because it only adds for example:



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


      I wanna get that result:



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


      CAUTION! If someone get's 2 or more days of leaves i cannot do that like this:



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


      I have to analyze every day like that example where i explained.



      I know that's more difficult question i cannot find any clue how to solve it. Can someone please what should i change or add. Thank you in advance for any help or advice.









      share







      New contributor




      Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      In mysql database i created "leave" table:



      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');


      "Workers" table:



      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');


      "Orders" table:



      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" table (I'm sorry for that's not in order):



      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` tinyint(1) 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');


      What i've done:



      I can to sumarize "Order time" of each other workers. 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'
      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;


      Then i get:



      +---------+---------+---------------+------------+------------+
      | FNAME | LNAME | NUMBER_ORDER | DESC_ORDER | ORDER TIME |
      +---------+---------+---------------+------------+------------+
      | DAVID | BUCS | TEST | TEST | 22:30:21 |
      +---------+---------+---------------+------------+------------+
      | JACK | FAR | TEST | TEST | 21:19:18 |
      +---------+---------+---------------+------------+------------+


      I've analyzed 2 days (20.03.2019 and 21.03.2019). Photos are in below:



      enter image description here



      enter image description here



      Now i need mysql query that sumarize leave time correctly and PROPORTIONALLY to pattern described in below:



      enter image description here



      for example for leave time where ID_WORKER = 26 (he has order "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'


      What I've tried: I've tried sum leave time by mysql query 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'
      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;


      But I'm afraid that isn't enough because it only adds for example:



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


      I wanna get that result:



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


      CAUTION! If someone get's 2 or more days of leaves i cannot do that like this:



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


      I have to analyze every day like that example where i explained.



      I know that's more difficult question i cannot find any clue how to solve it. Can someone please what should i change or add. Thank you in advance for any help or advice.







      mysql





      share







      New contributor




      Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 mins ago









      Prochu1991Prochu1991

      1




      1




      New contributor




      Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Prochu1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Prochu1991 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233711%2fsumarize-leave-time-proportionally-and-correctly-mysql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Prochu1991 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Prochu1991 is a new contributor. Be nice, and check out our Code of Conduct.













          Prochu1991 is a new contributor. Be nice, and check out our Code of Conduct.












          Prochu1991 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233711%2fsumarize-leave-time-proportionally-and-correctly-mysql%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...