473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sumarize leave time proportionally and correctly - mysql

n mysql database i created "leave" table:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `leave` ( 
  2. `ID_LEAVE` int(11) NOT NULL,
  3. `ID_WORKER` int(11) NOT NULL,
  4. `BEGIN_DATE` datetime DEFAULT NULL,
  5. `END_DATE` datetime DEFAULT NULL
  6. )
  7.  
  8. INSERT INTO `leave` 
  9. (`ID_LEAVE`, `ID_WORKER`, `BEGIN_DATE`, `END_DATE`) VALUES
  10. (3, 26, '2019-03-20 07:00:00', '2019-03-21 15:00:00'),
  11. (4, 22, '2019-03-20 07:00:00', '2019-03-20 15:00:00');
"Workers" table:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `workers` (
  2. `ID_WORKER` int(11) NOT NULL,
  3. `FNAME` varchar(20) NOT NULL,
  4. `LNAME` varchar(20) NOT NULL
  5. )
  6.  
  7. INSERT INTO `workers` (`ID_WORKER`, `FNAME`, `LNAME`) VALUES
  8. (22, 'ALAN', 'FAST'),
  9. (23, 'LEON', 'SPEED'),
  10. (24, 'ADAM', 'GREEN'),
  11. (25, 'DAVID', 'BUCS'),
  12. (26, 'JACK', 'FAR'),
  13. (27, 'ADAM', 'GAX'),
  14. (28, 'ANDREW', 'WORM');
"Orders" table:

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `orders` (
  2. `ID_ORDER` int(11) NOT NULL,
  3. `DESC_ORDER` varchar(50) NOT NULL,
  4. `NUMBER_ORDER` varchar(30) NOT NULL
  5. )
  6.  
  7. INSERT INTO `orders` (`ID_ORDER`, `DESC_ORDER`, `NUMBER_ORDER`) VALUES
  8. (19, 'TEST', 'TEST'),
  9. (20, 'TEST2', 'TEST2'),
  10. (21, 'TEST3', 'TEST3'),
  11. (22, 'TEST4', 'TEST4');
"Order_status" table (I'm sorry for that's not in order):

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `order_status` (
  2. `ID_STATUS` int(11) NOT NULL,
  3. `ID_ORDER` int(11) NOT NULL,
  4. `ID_WORKER` int(11) NOT NULL,
  5. `BEGIN_DATE` datetime NOT NULL,
  6. `END_DATE` datetime NOT NULL,
  7. `ORDER_DONE` tinyint(1) DEFAULT NULL
  8.  
  9. INSERT INTO `order_status` (`ID_STATUS`, `ID_ORDER`, `ID_WORKER`, `BEGIN_DATE`, `END_DATE`, `ORDER_DONE`) VALUES
  10. (30, 19, 26, '2019-03-18 06:40:21', '2019-03-18 15:22:32', 'NO'),
  11. (31, 19, 25, '2019-03-18 06:51:23', '2019-03-18 15:11:10', 'NO'),
  12. (32, 19, 26, '2019-03-20 06:23:32', '2019-03-20 15:33:11', 'NO'),
  13. (33, 19, 25, '2019-03-20 06:44:42', '2019-03-20 15:16:22', 'NO'),
  14. (34, 19, 26, '2019-03-22 06:44:32', '2019-03-22 11:44:54', 'YES'),
  15. (35, 19, 25, '2019-03-22 06:59:22', '2019-03-22 12:33:33', 'YES'),
  16. (42, 20, 22, '2019-03-18 06:44:23', '2019-03-18 15:11:23', 'NO'),
  17. (43, 20, 23, '2019-03-18 06:50:45', '2019-03-18 15:15:44', 'NO'),
  18. (44, 20, 22, '2019-03-19 06:40:20', '2019-03-19 15:23:30', 'NO'),
  19. (45, 20, 22, '2019-03-21 06:55:45', '2019-03-21 16:03:23', 'NO'),
  20. (46, 20, 22, '2019-03-22 06:55:45', '2019-03-22 13:23:44', 'YES'),
  21. (47, 20, 23, '2019-03-19 06:45:23', '2019-03-19 15:33:23', 'NO'),
  22. (48, 20, 23, '2019-03-20 06:45:23', '2019-03-20 15:22:23', 'NO'),
  23. (49, 20, 23, '2019-03-21 06:50:33', '2019-03-21 15:33:11', 'NO'),
  24. (50, 20, 23, '2019-03-22 06:44:23', '2019-03-22 12:22:44', 'YES'),
  25. (51, 21, 24, '2019-03-18 06:46:55', '2019-03-18 15:23:22', 'NO'),
  26. (52, 21, 24, '2019-03-19 06:55:33', '2019-03-19 15:23:33', 'NO'),
  27. (53, 21, 24, '2019-03-20 06:50:59', '2019-03-20 15:44:02', 'NO'),
  28. (54, 21, 24, '2019-03-21 06:44:33', '2019-03-21 15:23:33', 'YES'),
  29. (55, 21, 24, '2019-03-22 06:49:33', '2019-03-22 11:22:32', 'YES'),
  30. (56, 22, 27, '2019-03-18 06:50:12', '2019-03-18 15:22:15', 'NO'),
  31. (57, 22, 28, '2019-03-18 06:44:12', '2019-03-18 15:33:44', 'NO'),
  32. (58, 22, 27, '2019-03-19 06:53:22', '2019-03-19 15:22:33', 'NO'),
  33. (59, 22, 28, '2019-03-19 06:33:22', '2019-03-19 15:15:22', 'NO'),
  34. (60, 22, 28, '2019-03-21 06:33:22', '2019-03-21 15:01:56', 'NO'),
  35. (61, 22, 27, '2019-03-22 06:33:22', '2019-03-22 11:05:33', 'YES'),
  36. (62, 22, 27, '2019-03-21 06:45:22', '2019-03-21 15:33:33', 'NO'),
  37. (63, 22, 28, '2019-03-22 06:51:33', '2019-03-22 10:35:55', 'YES'),
  38. (64, 19, 25, '2019-03-19 06:50:32', '2019-03-19 15:33:44', 'NO'),
  39. (65, 19, 26, '2019-03-19 06:44:50', '2019-03-19 15:22:33', 'NO'),
  40. (66, 19, 25, '2019-03-21 06:50:50', '2019-03-21 15:33:33', 'NO');
  41. (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:

Expand|Select|Wrap|Line Numbers
  1. SELECT workers.FNAME, 
  2. workers.LNAME, 
  3. order_statusAgg.NUMBER_ORDER,
  4. order_statusAgg.DESC_ORDER, 
  5. SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'ORDER TIME'
  6. FROM workers
  7. LEFT JOIN (
  8. SELECT order_status.ID_WORKER, orders.NUMBER_ORDER, orders.DESC_ORDER, 
  9. SUM((TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))) AS stime 
  10. FROM order_status INNER JOIN orders
  11. ON orders.ID_ORDER = order_status.ID_ORDER
  12. GROUP BY order_status.ID_WORKER) order_statusAgg
  13. ON workers.ID_WORKER = order_statusAgg.ID_WORKER
  14. WHERE order_statusAgg.NUMBER_ORDER LIKE 'TEST'
  15. 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:

https://i.stack.imgur.com/ooDoz.jpg

https://i.stack.imgur.com/Kksge.jpg

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

https://i.stack.imgur.com/DyacM.jpg

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:

Expand|Select|Wrap|Line Numbers
  1. SELECT workers.FNAME, 
  2. workers.LNAME, 
  3. order_statusAgg.NUMBER_ORDER,
  4. order_statusAgg.DESC_ORDER, 
  5. SEC_TO_TIME(SUM(order_statusAgg.stime)) AS 'ORDER TIME', 
  6. IFNULL(SEC_TO_TIME(SUM(leaveAgg.ltime)),'00:00:00') AS 'LEAVE TIME'
  7. FROM workers
  8. 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
  9. LEFT JOIN
  10. (SELECT ID_WORKER, MIN(BEGIN_DATE) AS 'MIN_BEGIN_DATE', MAX(END_DATE) AS 'MAX_END_DATE' 
  11. FROM order_status GROUP BY ID_WORKER) ordstat ON 
  12. leave.ID_WORKER = ordstat.ID_WORKER 
  13. WHERE leave.END_DATE <= MAX_END_DATE AND leave.BEGIN_DATE >= MIN_BEGIN_DATE GROUP BY leave.ID_WORKER) leaveAgg
  14. ON leaveAgg.ID_WORKER = workers.ID_WORKER
  15. LEFT JOIN (
  16. SELECT order_status.ID_WORKER, orders.NUMBER_ORDER, orders.DESC_ORDER, 
  17. SUM((TIME_TO_SEC(order_status.END_DATE) - TIME_TO_SEC(order_status.BEGIN_DATE))) AS stime 
  18. FROM order_status INNER JOIN orders
  19. ON orders.ID_ORDER = order_status.ID_ORDER
  20. GROUP BY order_status.ID_WORKER) order_statusAgg
  21. ON workers.ID_WORKER = order_statusAgg.ID_WORKER
  22. WHERE order_statusAgg.NUMBER_ORDER LIKE 'TEST'
  23. 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.
Apr 1 '19 #1
1 1582
zmbd
5,501 Expert Mod 4TB
Prochotap,
I think that your images on i.stack.imgur.com may have caused your post to flag as spam.
Instead of linking to these images please use the [advanced] editor and attach the images to your thread.

BTW: Quite often images on sites such as imgur.com are unavailable to our experts when they are helping from their workplace - companies tend to block these sites.
May 1 '19 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Prabu Subroto | last post by:
Dear my friends... I am trying to develop a database application with PHP Version 4.3.2, MS Window 2000, MySQL 4.0.13-nt and Apache 2. I tried to insert a record onto my MySQL but I got this...
0
by: John R | last post by:
I found this old thread while looking for some type of compression solution myself. Long story short, I contacted Lester and ended up writing an LZO solution just as Mark had mentioned below....
1
by: Dave Posh | last post by:
I seem to be having a problem displaying time stored in mysql. The format stored in the database is 13:15:05. The database data type is time. I'm using asp vbscript and sql to retrieve the time...
3
by: bobmct | last post by:
In my feeble attempt to keep track of login session timeouts I have the following code in my login section of my program: $sql = "UPDATE subscriber SET _sessexp = 'DATE_ADD(NOW(),INTERVAL 15...
2
by: ojorus | last post by:
Hi! A simple question maybe... I have a datetime column in a table, and want to automaticly insert local (Norwegian) time into the column. (I use PHP 5) I can not use NOW() since the mysql...
1
by: rija | last post by:
Hi folks, I need help regarding date and time comparison in PHP and MySQL My website is hosted in the USA (GMT - 8) And It is managed in Madagascar (GMT + 3) Want to consider DATE in Madagascar...
5
by: rouven | last post by:
Hi, i am trying to convert that time format '05:26:40 Jun 19, 2007 PDT' into mysql compatible format like YYYY-MM-DD HH:MM:SS. the code i tried was: from datetime import datetime from time...
6
by: ponvijaya | last post by:
hi all, I have time stored in my table field as hh:mm am or pm (eg 04:15 PM , 02:30 AM). The field is of type varchar. I need to convert it as a MySql time format. Can any body help...
6
by: Geoff Cox | last post by:
Hello, at the moment I can add the combined date and time into MySQL using php $dt1 = date("Y-m-d H:i:s"); is it possible to add the date and time separately? I thought it might be
0
by: Syed Khaleel Ahamed | last post by:
Dear Sir, I want to get the difference of date/time captured in mysql and present date/time in jsp. I am using the following code to get, but its getting difference only in hrs, i used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.