Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Paso 1: Filtrar los datos relevantes
- CREATE TEMPORARY TABLE tmp_filtrados AS
- SELECT
- idpersona,
- idperiodo
- FROM
- d_deuda
- WHERE
- idperiodo BETWEEN 226 AND 233
- AND idconcepto = 6;
- -- Paso 2: Generar números correlativos por idpersona ordenados por idperiodo
- SET @row_number := 0;
- SET @current_idpersona := NULL;
- CREATE TEMPORARY TABLE tmp_correlativos AS
- SELECT
- idpersona,
- idperiodo,
- (@row_number := IF(@current_idpersona = idpersona, @row_number + 1, 1)) AS rn,
- (@current_idpersona := idpersona) AS dummy
- FROM
- tmp_filtrados
- ORDER BY
- idpersona, idperiodo;
- -- Paso 3: Calcular la diferencia entre idperiodo y rn para detectar rupturas
- CREATE TEMPORARY TABLE tmp_diferencias AS
- SELECT
- idpersona,
- idperiodo,
- rn,
- idperiodo - rn AS diferencia
- FROM
- tmp_correlativos;
- -- Paso 4: Crear una tabla temporal para almacenar el conteo de cada grupo
- CREATE TEMPORARY TABLE tmp_rupturas AS
- SELECT
- idpersona,
- diferencia,
- COUNT(*) AS cuenta
- FROM
- tmp_diferencias
- GROUP BY
- idpersona, diferencia;
- -- Paso 5: Detectar las rupturas comparando el total de filas por idpersona
- SELECT DISTINCT
- d.idpersona
- FROM
- tmp_diferencias d
- LEFT JOIN
- tmp_rupturas r ON d.idpersona = r.idpersona
- GROUP BY
- d.idpersona
- HAVING
- SUM(r.cuenta) < COUNT(d.idperiodo);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement