Skip to main content
3 de 3
se añadieron 2659 caracteres en el cuerpo
Monclus
  • 433
  • 2
  • 11

Buenos días!

Para empezar creo que lo que necesitas es la funcion HOUR()de MySQL, que te devuelve la hora. Incluso si quieres hacerlo por franjas de media hora puedes utilizar la funcion MINUTE() para obtener los minutos de un timestamp.

Una vez con esto puedes hacer la consulta sumando los registros que cumplan los IFs, así tendrás la suma por franja horaria.

Por ejemplo, para franjas horarias de horas enteras tendrías algo así:

SELECT 
SUM(if(HOUR(T.TIME_CONTACT) = 0, 1, 0)) AS FRANJA_0,
SUM(if(HOUR(T.TIME_CONTACT) = 1, 1, 0)) AS FRANJA_1,
SUM(if(HOUR(T.TIME_CONTACT) = 2, 1, 0)) AS FRANJA_2,
SUM(if(HOUR(T.TIME_CONTACT) = 3, 1, 0)) AS FRANJA_3,
SUM(if(HOUR(T.TIME_CONTACT) = 4, 1, 0)) AS FRANJA_4,
SUM(if(HOUR(T.TIME_CONTACT) = 5, 1, 0)) AS FRANJA_5,
SUM(if(HOUR(T.TIME_CONTACT) = 6, 1, 0)) AS FRANJA_6,
SUM(if(HOUR(T.TIME_CONTACT) = 7, 1, 0)) AS FRANJA_7,
SUM(if(HOUR(T.TIME_CONTACT) = 8, 1, 0)) AS FRANJA_8,
SUM(if(HOUR(T.TIME_CONTACT) = 9, 1, 0)) AS FRANJA_9,
SUM(if(HOUR(T.TIME_CONTACT) = 10, 1, 0)) AS FRANJA_10,
SUM(if(HOUR(T.TIME_CONTACT) = 11, 1, 0)) AS FRANJA_11,
SUM(if(HOUR(T.TIME_CONTACT) = 12, 1, 0)) AS FRANJA_12,
SUM(if(HOUR(T.TIME_CONTACT) = 13, 1, 0)) AS FRANJA_13,
SUM(if(HOUR(T.TIME_CONTACT) = 14, 1, 0)) AS FRANJA_14,
SUM(if(HOUR(T.TIME_CONTACT) = 15, 1, 0)) AS FRANJA_15,
SUM(if(HOUR(T.TIME_CONTACT) = 16, 1, 0)) AS FRANJA_16,
SUM(if(HOUR(T.TIME_CONTACT) = 17, 1, 0)) AS FRANJA_17,
SUM(if(HOUR(T.TIME_CONTACT) = 18, 1, 0)) AS FRANJA_18,
SUM(if(HOUR(T.TIME_CONTACT) = 19, 1, 0)) AS FRANJA_19,
SUM(if(HOUR(T.TIME_CONTACT) = 20, 1, 0)) AS FRANJA_20,
SUM(if(HOUR(T.TIME_CONTACT) = 21, 1, 0)) AS FRANJA_21,
SUM(if(HOUR(T.TIME_CONTACT) = 22, 1, 0)) AS FRANJA_22,
SUM(if(HOUR(T.TIME_CONTACT) = 23, 1, 0)) AS FRANJA_23
FROM DIFERIDAS T;

Bueno y ya para acabar de rizar el rizo esta seria la consulta para ver las llamadas por franjas de media hora desde las 8.00 hasta las 18.00:

SELECT 
SUM(if(HOUR(T.TIME_CONTACT) = 8 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_8_00,
SUM(if(HOUR(T.TIME_CONTACT) = 8 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_8_30,
SUM(if(HOUR(T.TIME_CONTACT) = 9 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_9_00,
SUM(if(HOUR(T.TIME_CONTACT) = 9 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_9_30,
SUM(if(HOUR(T.TIME_CONTACT) = 10 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_10_00,
SUM(if(HOUR(T.TIME_CONTACT) = 10 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_10_30,
SUM(if(HOUR(T.TIME_CONTACT) = 11 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_11_00,
SUM(if(HOUR(T.TIME_CONTACT) = 11 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_11_30,
SUM(if(HOUR(T.TIME_CONTACT) = 12 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_12_00,
SUM(if(HOUR(T.TIME_CONTACT) = 12 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_12_30,
SUM(if(HOUR(T.TIME_CONTACT) = 13 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_13_00,
SUM(if(HOUR(T.TIME_CONTACT) = 13 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_13_30,
SUM(if(HOUR(T.TIME_CONTACT) = 14 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_14_00,
SUM(if(HOUR(T.TIME_CONTACT) = 14 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_14_30,
SUM(if(HOUR(T.TIME_CONTACT) = 15 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_15_00,
SUM(if(HOUR(T.TIME_CONTACT) = 15 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_15_30,
SUM(if(HOUR(T.TIME_CONTACT) = 16 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_16_00,
SUM(if(HOUR(T.TIME_CONTACT) = 16 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_16_30,
SUM(if(HOUR(T.TIME_CONTACT) = 17 and MINUTE(T.TIME_CONTACT) >= 0 and MINUTE(T.TIME_CONTACT) <= 29, 1, 0)) AS FRANJA_17_00,
SUM(if(HOUR(T.TIME_CONTACT) = 17 and MINUTE(T.TIME_CONTACT) >= 30 and MINUTE(T.TIME_CONTACT) <= 59, 1, 0)) AS FRANJA_17_30
FROM DIFERIDAS T;
Monclus
  • 433
  • 2
  • 11