Ver Mensaje Individual
  #6 (permalink)  
Antiguo 10/02/2015, 02:13
Avatar de alexmetola
alexmetola
 
Fecha de Ingreso: julio-2013
Ubicación: madrid
Mensajes: 33
Antigüedad: 10 años, 10 meses
Puntos: 0
Respuesta: Consulta complicada Fechas

Bueno al final he decidido por hacer un procedimiento con 2 cursores analizando asi todas las combinaciones, igualmente si hay alguna forma de hacerlo mejor decirmelo.

Os dejo el codigo que he realizado:

Código MySQL:
Ver original
  1. -- --------------------------------------------------------------------------------
  2. -- Routine DDL
  3. -- Note: comments before and after the routine body will not be stored by the server
  4. -- --------------------------------------------------------------------------------
  5. DELIMITER $$
  6.  
  7. CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckRed`()
  8.  
  9.  
  10.   DECLARE done,done2 INT DEFAULT FALSE;
  11.   DECLARE Asignedc,Asigned2c int;
  12.   DECLARE IdComp varchar(500);
  13.    
  14.   DECLARE StartTimec,StartTime2c datetime;
  15.   DECLARE EndTimec,EndTime2c datetime;
  16.   DECLARE Idc,Id2c INT;
  17.   DECLARE cur1 CURSOR FOR SELECT Id,Asigned,StartTime,EndTime  FROM Prueba.MyTable;
  18.   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  19.     CREATE TEMPORARY TABLE IF NOT exists temp1 ( Id int);
  20.   OPEN cur1;
  21. SET IdComp = '0';
  22.  
  23.   read_loop: LOOP
  24.     FETCH cur1 INTO Idc,Asignedc,StartTimec,EndTimec;
  25.    IF done THEN
  26.       LEAVE read_loop;
  27.     END IF;
  28. If Idc not in (IdComp) then
  29.             BLOCK2: BEGIN
  30.                     DECLARE cur2 CURSOR FOR SELECT Id,Asigned,StartTime,EndTime FROM Prueba.MyTable where Asigned=Asignedc
  31.                             and Id!=Idc and StartTimec<=StartTime and EndTimec>=StartTime and Id not in (IdComp);
  32.                     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = TRUE;
  33.                     OPEN cur2;
  34.                     read_loop2: LOOP
  35.                     FETCH FROM cur2 INTO Id2c,Asigned2c,StartTime2c,EndTime2c;  
  36.                         IF done2 THEN
  37.                         set done2 = false;
  38.                         CLOSE cur2;
  39.                         LEAVE read_loop2;
  40.                         END IF;
  41.                         INSERT INTO temp1 VALUES (Id2c);
  42.                        
  43.                         SET IdComp = CONCAT(Id2c,',', IdComp);
  44.                        
  45.                     END LOOP read_loop2;
  46.             END BLOCK2;
  47.   END LOOP;
  48.   CLOSE cur1;
  49. select * from temp1;
  50. drop table temp1;

Saludos y gracias por todo.