Ver Mensaje Individual
  #7 (permalink)  
Antiguo 30/12/2011, 06:22
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 8 meses
Puntos: 574
Respuesta: Consulta en MYSQL sacar secuencia

Quizas esta es mejor.... analizala....

(de la misma fuente que la anterior)

cambiando maquinas por empleados es que el ejemplo es lo que estas buscando!!!!

Código pre:
Ver original
  1. Gaps in a time series
  2.  
  3. Advanced time series analysis generally requires custom software, but
  4. straightforward SQL queries can answer simple time series questions.
  5. You have a jobtimes table with columns ID, job, machine, start_time, and
  6. stop_time. You wish to know which machines have had gaps between activity
  7. periods. It's a version of "Find available booking periods":
  8.  
  9. drop table jobtimes;
  10. create table jobtimes(id int, machine smallint, start_time timestamp, stop_time timestamp);
  11. insert into jobtimes values(1,1,'2011-7-1 08:00:00', '2011-7-1 10:00:00');
  12. insert into jobtimes values(2,1,'2011-7-1 11:00:00', '2011-7-1 14:00:00');
  13. insert into jobtimes values(3,2,'2011-7-1 08:00:00', '2011-7-1 09:00:00');
  14. insert into jobtimes values(4,2,'2011-7-1 09:00:00', '2011-7-1 10:00:00');
  15. insert into jobtimes values(5,3,'2011-7-1 08:00:00', '2011-7-1 08:30:00');
  16. insert into jobtimes values(6,3,'2011-7-1 10:00:00', '2011-7-1 12:00:00');
  17. select * from jobtimes;
  18. +------+---------+---------------------+---------------------+
  19. | id   | machine | start_time          | stop_time           |
  20. +------+---------+---------------------+---------------------+
  21. |    1 |       1 | 2011-07-01 08:00:00 | 2011-07-01 10:00:00 |
  22. |    2 |       1 | 2011-07-01 11:00:00 | 2011-07-01 14:00:00 |
  23. |    3 |       2 | 2011-07-01 08:00:00 | 2011-07-01 09:00:00 |
  24. |    4 |       2 | 2011-07-01 09:00:00 | 2011-07-01 10:00:00 |
  25. |    5 |       3 | 2011-07-01 08:00:00 | 2011-07-01 08:30:00 |
  26. |    6 |       3 | 2011-07-01 10:00:00 | 2011-07-01 12:00:00 |
  27. +------+---------+---------------------+---------------------+
  28.  
  29. SELECT
  30.   a.machine,
  31.   a.stop_time AS 'Unused From',
  32.   Min(b.start_time) AS 'To'
  33. FROM jobtimes AS a
  34. JOIN jobtimes AS b ON a.machine=b.machine AND a.stop_time < b.start_time
  35. GROUP BY a.stop_time
  36. HAVING a.stop_time < MIN(b.start_time)
  37. ORDER BY machine;
  38. +---------+---------------------+---------------------+
  39. | machine | Unused From         | To                  |
  40. +---------+---------------------+---------------------+
  41. |       1 | 2011-07-01 10:00:00 | 2011-07-01 11:00:00 |
  42. |       3 | 2011-07-01 08:30:00 | 2011-07-01 10:00:00 |
  43. +---------+---------------------+---------------------+
__________________
Quim
--------------------------------------------------
Ayudar a ayudar es una buena práctica!!! Y da buenos resultados.