1

Tengo la siguiente situación, tengo una tabla donde estoy manejando datos de personas, que los estoy guardado por meses y por tipo de aseguradora. La cantidad de datos que estoy guardando por cada persona en un mes puede variar desde 1 hasta n. Esos datos lo tengo que colocar en un gráfico por lo que necesito que me queden de la siguiente forma:

enero, febrero, marzo ... diciembre

Realizando subconsulta los obtengo pero se me demora mucho, las operaciones a realizar. Tengo colocados los index en los campos de búsqueda y de la suma y aun así se me hace muy lenta puede tardar hasta 15 min, en la tabla estoy manejando al rededor de unos 15000 registro. A continuación le dejo la consulta para ver si me pueden ayudar.

Gracias.

    SELECT @var2 := tabla1.id_insured AS id_insured, @var1 := tabla1.pyear AS anio,
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '01') AS ene, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '02') AS feb, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '03') AS mar, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '04') AS abr, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '05') AS may, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '06') AS jun, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '07') AS jul, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '08') AS ago, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '09') AS sep, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '10') AS octb, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '11') AS nov, 
    (SELECT SUM(tabla1.commission) FROM tabla1 WHERE tabla1.id_insured = @var2 AND tabla1.pyear = @var1 AND tabla1.month = '12') AS dic 
    FROM tabla1 
    WHERE tabla1.pyear = '2018'
    GROUP BY tabla1.id_insured 
1
  • utiliza la clausla group by tabla1.month para agrupar por el mismo numero de mes y un DateName( month , DateAdd( month , tabla1.month, -1 )) para mostrar los nombres en vez de numeros
    – Bryro
    Commented el 13 feb. 2019 a las 20:28

2 respuestas 2

2

Creo que estas encarando mal el problema. Según te entiendo, lo que buscas es una tabla dónde puedas ver cada asegurado y la suma de las comisiones de cada mes de éstos, en 12 columnas. Una forma bien sencilla es usando una suma condicional y agrupando por el asegurado y el año (Eventualmente el año se puede ir por que estarías listando solo uno)

SELECT  t.id_insured    AS id_insured,
        t.pyear         AS anio
        SUM(CASE WHEN t.month = '01' THEN t.commission ELSE 0 END) AS ene,
        SUM(CASE WHEN t.month = '02' THEN t.commission ELSE 0 END) AS feb,
        SUM(CASE WHEN t.month = '03' THEN t.commission ELSE 0 END) AS mar,
        SUM(CASE WHEN t.month = '04' THEN t.commission ELSE 0 END) AS abr,
        SUM(CASE WHEN t.month = '05' THEN t.commission ELSE 0 END) AS may,
        SUM(CASE WHEN t.month = '06' THEN t.commission ELSE 0 END) AS jun,
        SUM(CASE WHEN t.month = '07' THEN t.commission ELSE 0 END) AS jul,
        SUM(CASE WHEN t.month = '08' THEN t.commission ELSE 0 END) AS ago,
        SUM(CASE WHEN t.month = '09' THEN t.commission ELSE 0 END) AS sep,
        SUM(CASE WHEN t.month = '10' THEN t.commission ELSE 0 END) AS octb,
        SUM(CASE WHEN t.month = '11' THEN t.commission ELSE 0 END) AS nov,
        SUM(CASE WHEN t.month = '12' THEN t.commission ELSE 0 END) AS dic
        FROM tabla1 t
        WHERE t.pyear = '2018'
        GROUP BY t.id_insured,
                 t.pyear
0

Hace un tiempo puse está pregunta y no había podio mejor su rendimiento de mi Query hasta que encontré esta explicación en un foro así que aquí se las dejo por si alguien mas le suce lo mismo. El proceso que se realiza es ir uniendo las subconsutas con join. a continuación les dejo el ejemplo:

SELECT payid.id_insured, payid.c_name as nombre, payid.pyear as anio ,payid.id, ene.enecom as enecom ,feb.febcom as febcom,mar.marcom as marcom ,abr.abrcom as abrcom, may.maycom as maycom, jun.juncom as juncom, jul.julcom as julcom, ago.agocom as agocom, sep.sepcom as sepcom, oct.octcom as octcom, nov.novcom as novcom, dic.diccom as diccom
    FROM (SELECT tabla1.id_insured, tabla1.c_name, tabla1.pyear, tabla1.p_number AS id  FROM tabla1 GROUP BY tabla1.p_number ) AS payid LEFT JOIN 
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS enecom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '01' GROUP BY tabla1.p_number, tabla1.month) ene ON payid.id = ene.id LEFT JOIN 
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS febcom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '02' GROUP BY tabla1.p_number, tabla1.month) feb ON payid.id = feb.id LEFT JOIN 
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS marcom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '03' GROUP BY tabla1.p_number, tabla1.month) mar ON payid.id = mar.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS abrcom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '04' GROUP BY tabla1.p_number, tabla1.month) abr ON payid.id = abr.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS maycom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '05' GROUP BY tabla1.p_number, tabla1.month) may ON payid.id = may.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS juncom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '06' GROUP BY tabla1.p_number, tabla1.month) jun ON payid.id = jun.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS julcom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '07' GROUP BY tabla1.p_number, tabla1.month) jul ON payid.id = jul.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS agocom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '08' GROUP BY tabla1.p_number, tabla1.month) ago ON payid.id = ago.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS sepcom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '09' GROUP BY tabla1.p_number, tabla1.month) sep ON payid.id = sep.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS octcom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '10' GROUP BY tabla1.p_number, tabla1.month) oct ON payid.id = oct.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS novcom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '11' GROUP BY tabla1.p_number, tabla1.month) nov ON payid.id = nov.id LEFT JOIN
    (SELECT tabla1.p_number AS id, SUM(tabla1.commission) AS diccom FROM tabla1 WHERE  tabla1.pyear = '2019' AND tabla1.month = '12' GROUP BY tabla1.p_number, tabla1.month) dic ON payid.id = dic.id

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.