×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to sumarize “sickness time” to the end of order - mysql

How to sumarize “sickness time” to the end of order - mysql

How to sumarize “sickness time” to the end of order - mysql

(OP)
In mysql database i've created "sickness" table:

CODE

+--------+---------+---------+-------------+---------+-------------------------------+---------------+
| Id_SICK|ID_WORKER| FNAME   | LNAME   | BEGIN_DATE          | END_DATE              | SICKNESS_TIME |
+--------+---------+---------+---------+------------+--------------------+-----------+---------------+
| 6      |   17    | PAUL    | KING    |2019-03-19 07:00:00  |2019-03-20 15:00:00    |    16:00:00   | 
| 7      |   17    | PAUL    | KING    |2019-03-25 07:00:00  |2019-03-25 15:00:00    |    8:00:00    |
+--------+---------+---------+----------------------+--------------------------------+---------------+ 


"Workers" table:

CODE -->

+----------+---------+---------+
|ID_WORKER |  FNAME  | LNAME   |
+----------+---------+----------
| 17       |  PAUL   |  KING   |
| 18       |  SAM    |  BULK   |
+----------+---------+---------+ 

"Orders" table:

CODE -->

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

"Order_status" table:

CODE -->

+----------+---------+---------+---------------------+-------------------+------------+
| Id_status|ID_WORKER| ID_ORDER| BEGIN_DATE          | END_DATE          | ORDER_DONE |
+----------+---------+---------+----------+------------+---------+--------------------+
| 47       |   17    |    20   |2019-03-18 06:50:35  |2019-03-18 15:21:32|  NO        |
| 48       |   17    |    20   |2019-03-20 06:44:12  |2019-03-20 15:11:23|  NO        |
| 50       |   17    |    20   |2019-03-22 06:50:20  |2019-03-22 12:22:33|  YES       |
| 51       |   18    |    20   |2019-03-18 06:45:11  |2019-03-18 15:14:45|  NO        |
| 52       |   18    |    20   |2019-03-20 06:50:22  |2019-03-20 15:10:32|  NO        |
| 53       |   18    |    20   |2019-03-22 06:54:11  |2019-03-22 11:23:45|  YES       |
+----------+---------+---------+------------+---------+-------------------+-----------+ 

What i've done:

I can to sumarize "total time" of each other workers (in order_status table) on the order including with sumarizing "sickness time" from Sickness table. 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:

CODE --> 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(sickAgg.vtime)),'00:00:00') AS 'LEAVE TIME'
FROM workers 
LEFT JOIN (
SELECT leave.id_worker, SUM((datediff(sickness.end_date, sickness.begin_date) + 1) * (time_to_sec(time(sickness.end_date)) - time_to_sec(time(sickness.begin_date)))) AS vtime 
FROM sickness
GROUP BY sickness.id_worker) sickAgg
           ON sickAgg.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; 

Then i get:

CODE -->

+---------+---------+---------------+------------+------------+--------------+
|  FNAME  | LNAME   |  NUMBER_ORDER | DESC_ORDER | TOTAL TIME | Sickness_time| 
+---------+---------+---------------+------------+------------+--------------+
|  PAUL   |  KING   | TEST          | TEST       | 22:30:21   |   24:00:00   |   
|  SAM    |  BULK   | TEST          | TEST       | 21:19:18   |   00:00:00   |   
+---------+---------+---------------+------------+------------+--------------+ 

Okey but on the other hand that order was finished in 23-03-2019. PAUL KING had a sickness in 25-03-2019 too and his sickness time shouldn't added during this order which he was doing. So in this case that should be:

CODE --> mysql

+---------+---------+---------------+------------+------------+--------------+
|  FNAME  | LNAME   |  NUMBER_ORDER | DESC_ORDER | TOTAL TIME | Sickness_time| 
+---------+---------+---------------+------------+------------+--------------+
|  PAUL   |  KING   | TEST          | TEST       | 22:30:21   |   16:00:00   |   
|  SAM    |  BULK   | TEST          | TEST       | 21:19:18   |   00:00:00   |   
+---------+---------+---------------+------------+------------+--------------+ 

I'm wondering if that is about that code issue?

CODE --> mysql

LEFT JOIN (
SELECT leave.id_worker, SUM((datediff(sickness.end_date, sickness.begin_date) + 1) * (time_to_sec(time(sickness.end_date)) - time_to_sec(time(sickness.begin_date)))) AS vtime 
FROM sickness
GROUP BY sickness.id_worker) sickAgg
           ON sickAgg.id_worker = workers.id_worker 

Has someone ideas how to deal with summarizing it till the end of duration of the order? Is it that possible? I was searching any idea but I'm not mysql guru. Thank you for any help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close