Note that there are some explanatory texts on larger screens.

plurals
  1. POHow group the employees in ranges of hours?
    primarykey
    data
    text
    <p>I have this table to save the time the employees spend doing a routine task.</p> <pre><code>CREATE TABLE tasks ( id INT NOT NULL PRIMARY KEY, name VARCHAR(100), date_task date, time_ini time, time_end time ); </code></pre> <ul> <li>I'm trying to group the employees who have at least two time_ini's with a difference &lt; 15 minutes with any employee's time_ini or time_end.</li> <li>If none of their time_inis meet this condition, then this employee would be grouped alone.</li> <li>The groups will be numbered from 1 to n. </li> <li>And then the groups will be ordered by date ascending, and time_ini ascending.</li> </ul> <p>This is an example of data:</p> <pre><code>(1, "oscar", '2012-01-01', '01:30', '01:32'), (2, "oscar", '2012-01-01', '02:30', '02:32'), (3, "oscar", '2012-01-01', '05:30', '05:32'), (4, "oscar", '2012-01-01', '06:30', '06:32'), (5, "mario", '2012-01-01', '02:43', '02:43'), (6, "mario", '2012-01-01', '02:53', '02:53'), (7, "mario", '2012-01-01', '05:30', '05:30'), (8, "martah", '2012-01-01', '01:25', '01:28'), (9, "martah", '2012-01-01', '02:29', '02:41'), (10, "jesus", '2012-01-01', '01:25', '01:28'), (11, "jesus", '2012-01-01', '01:25', '02:28'), (12, "jesus", '2012-01-01', '07:33', '08:32'), (13, "jesus", '2012-01-01', '07:35', '07:36'), (14, "jesus", '2012-01-01', '08:36', '08:39'), (15, "rober", '2012-01-01', '02:43', '02:46'), (16, "rober", '2012-01-01', '02:56', '03:00'), (17, "rober", '2012-01-01', '02:29', '11:32'), (18, "pedro", '2012-01-01', '11:36', '12:46'), (19, "pedro", '2012-01-01', '12:36', '16:46'); </code></pre> <p>This would be the result:</p> <pre><code>GROUP NAME 1 oscar 1 marta 1 jesus 2 mario 2 rober 3 pedro </code></pre> <p>I came up with something like this:</p> <pre><code>select distinct a.name from tasks a where (select count(id) from tasks b where ( MINUTE(TIMEDIFF(a.time_ini, b.time_ini)) &lt; 15 OR MINUTE(TIMEDIFF(a.time_end, b.time_ini)) &lt; 15 ) and b.name &lt;&gt; a.name) &gt;= 2; </code></pre> <p>I'm afraid I can't group them this way, but I think I'm not too far to the solution, isn't it? </p> <p>Any idea, tip or advice will be appreciated, and if you need more info, let me know and I'll edit the post. Is little bit hard to explain...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload