1

Tengo una tabla que guarda la fecha y hora de los accesos a un sitio web.

la tabla guarda la fecha en formato 000-00-00 00:00:00 y la formateo con

DATE_FORMAT(ultimo_login,'%Y/%m/%d')

todo correcto hasta aquí

SELECT
COUNT(DATE_FORMAT(ultimo_login,'%Y/%m/%d')) AS contador
, DATE_FORMAT(ultimo_login,'%Y/%m/%d') AS concepto
FROM userslogins
GROUP BY concepto;

El caso es que me faltan fechas entre unas y otras, y por mas que lo intento no logro rellenarlas.

la consulta aparece así

introducir la descripción de la imagen aquí

y tendría que ser

introducir la descripción de la imagen aquí

¿Alguien podría ayudarme con esto? Mil gracias

NOTA: las fechas que faltan son por que no se ha ejecutado nada ese día, no por que falle el ingresarlas...

7
  • 1
    Por que no guardas el campo como TimeSpam ? el 27 dic. 2019 a las 21:22
  • 1
    faltan fechas por que no se han generado... esos dias no se abrió la aplicacion
    – SPock
    el 27 dic. 2019 a las 21:32
  • 2
    ¿Responde esto a tu pregunta? ¿Cómo obtener fechas consecutivas faltantes en MySql?
    – user128299
    el 27 dic. 2019 a las 21:35
  • 1
    si y no...es con lo que he estado tratando de solucionarlo pero no he sido capaz de implementarlo
    – SPock
    el 27 dic. 2019 a las 21:38
  • 1
    añada el contado al group by, es decir group by concepto, contador
    – Pavlo B.
    el 27 dic. 2019 a las 21:52

1 respuesta 1

1

Lo que deberías de hacer es generar una tabla temporal con el rango de fechas que necesitas

este es un proceso almacenado que puedes ejecutar para crear la tabla temporal con los registros, con esto podrias hacer un subquery a la tabla de userslogins


# generar la tabla temporal entre el rango deseado
call make_intervals(
  (SELECT MIN(ultimo_login) FROM userslogins),
  (SELECT MAX(ultimo_login) FROM userslogins),
  1,
  'DAY'
);

# consulta deseada
select 
  (SELECT COUNT(*) FROM userslogins WHERE DATE(ultimo_login) = DATE(interval_start)) AS contador, 
  DATE_FORMAT(interval_start,'%Y/%m/%d') AS concepto
from time_intervals;
DELIMITER $$

USE `database`$$

DROP PROCEDURE IF EXISTS `make_intervals`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `make_intervals`(startdate timestamp, enddate timestamp, intval integer, unitval varchar(10))
deterministic
BEGIN
        declare thisDate timestamp;
       declare nextDate timestamp;
       set thisDate = startdate;
       -- *************************************************************************
       -- Drop / create the temp table
       -- *************************************************************************
       drop temporary table if exists time_intervals;
       create temporary table if not exists time_intervals
          (
          interval_start timestamp,
          interval_end timestamp
          );
       -- *************************************************************************
       -- Loop through the startdate adding each intval interval until enddate
       -- *************************************************************************
        repeat
            select
                case unitval
                    when 'MICROSECOND' then timestampadd(MICROSECOND, intval, thisDate)
                    when 'SECOND'      then timestampadd(SECOND, intval, thisDate)
                    when 'MINUTE'      then timestampadd(MINUTE, intval, thisDate)
                    when 'HOUR'        then timestampadd(HOUR, intval, thisDate)
                when 'DAY'         then timestampadd(DAY, intval, thisDate)
                when 'WEEK'        then timestampadd(WEEK, intval, thisDate)
                when 'MONTH'       then timestampadd(MONTH, intval, thisDate)
                when 'QUARTER'     then timestampadd(QUARTER, intval, thisDate)
                when 'YEAR'        then timestampadd(YEAR, intval, thisDate)
            end into nextDate;
            insert into time_intervals select thisDate, timestampadd(MICROSECOND, -1, nextDate);
            set thisDate = nextDate;
            until thisDate >= enddate
       end repeat;
    END$$

DELIMITER ;

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.