I have this query, the problem I have is the following, the field:
@acum:= @acum + count(mtg.idTemaGestion) as acumulado
is not accumulating the data, it is just showing the
count(mtg.idTemaGestion)
Try both ways using
join (SELECT @acum:= 0) r
And using a
set @acum:= 0;
But in both examples, the same thing happens to me.
Complete consultation:
SELECT *, round((c.acumulado / total), 2) as pareto
from (
SELECT tg.detTipoGestion, mtg.detTemaGestion,
(SELECT sum(total) from totalParaCalcular) as total,
count(mtg.idTemaGestion) as totalRegistros,
@acum:= @acum + count(mtg.idTemaGestion) as acumulado
from mac_gestion mg
inner join mac360_mac_gestion mmg on mg.idMacGestion = mmg.idMacGestion_FK
inner join mac360 m on m.idMac360 = mmg.idMac360_FK
inner join mac_tipogestion tg on tg.idTipoGestion = mg.idTipoGestion_FK
inner join datos_nomina dn on dn.idDatosNomina = m.idDatosNominaRecibe_FK
inner join site si on si.idSite = dn.idSite_FK
inner join cuenta_proceso_celula cpc on cpc.idCuentaProcesoCelula =
dn.idCuentaProcesoCelula_FK
inner join cuenta cu on cu.idCuenta = cpc.idCuenta_FK
inner join proceso pro on pro.idProceso = cpc.idProceso_FK
inner join celula ce on ce.idCelula = cpc.idCelula_FK
inner join mac_temagestion mtg on mtg.idTemaGestion = mg.idTemaGestion_FK
join (SELECT @acum:= 0) r
where mg.error = 'si'
and si.idSIte in (2)
and cu.idCuenta in (2)
and pro.idProceso in (25)
and ce.idCelula in (12)
and tg.idTipoGestion in (1)
group by mtg.detTemaGestion
order by count(mtg.idTemaGestion) desc
) c
WHERE C.ACUMULADO/Total <= 0.80;
In the following image is the print of the results that I receive and how I would have to receive them, since this data does not accumulate, everything else fails me because several operations depend on that field being ok:
I think the problem is due to the join's because when trying one example with a single table works correctly, I'm doing temporary tables to bring the data from another side but it always returns the same, it does not accumulate the info
UPDATE
Well I leave the question open to see that item because I just did it but the only way I found was creating two temporary tables to go calculating the data and sure it has a different solution but I can not find it:
CREATE TEMPORARY TABLE totalParaCalcular (dato nvarchar(100), total int);
INSERT INTO totalParaCalcular
SELECT mtg.idTemaGestion, count(mtg.idTemaGestion) as total
from mac_gestion mg
inner join mac360_mac_gestion mmg on mg.idMacGestion = mmg.idMacGestion_FK
inner join mac360 m on m.idMac360 = mmg.idMac360_FK
inner join mac_tipogestion tg on tg.idTipoGestion = mg.idTipoGestion_FK
inner join datos_nomina dn on dn.idDatosNomina = m.idDatosNominaRecibe_FK
inner join site si on si.idSite = dn.idSite_FK
inner join cuenta_proceso_celula cpc on cpc.idCuentaProcesoCelula =
dn.idCuentaProcesoCelula_FK
inner join cuenta cu on cu.idCuenta = cpc.idCuenta_FK
inner join proceso pro on pro.idProceso = cpc.idProceso_FK
inner join celula ce on ce.idCelula = cpc.idCelula_FK
inner join mac_temagestion mtg on mtg.idTemaGestion = mg.idTemaGestion_FK
where mg.error = 'si'
and si.idSIte in (2)
and cu.idCuenta in (2)
and pro.idProceso in (25)
and ce.idCelula in (12)
and tg.idTipoGestion in (1)
group by mtg.detTemaGestion
order by total desc;
CREATE TEMPORARY TABLE totalParaCalcularConAcumulado (idDato int, total int,
acumulado int);
SET @acum:= 0;
INSERT INTO totalParaCalcularConAcumulado
SELECT dato, total, @acum:= @acum + total as acumulado
from totalParaCalcular
join (SELECT @acum=0) r;
SELECT *, round((c.acumulado / total), 2) as pareto
from (
SELECT mtg.idTemaGestion as id, tg.detTipoGestion, mtg.detTemaGestion,
(SELECT sum(total) from totalParaCalcular) as total,
(SELECT acumulado from totalParaCalcularConAcumulado where idDato = id) as
acumulado,
count(mtg.idTemaGestion) as totalRegistros
from mac_gestion mg
inner join mac360_mac_gestion mmg on mg.idMacGestion = mmg.idMacGestion_FK
inner join mac360 m on m.idMac360 = mmg.idMac360_FK
inner join mac_tipogestion tg on tg.idTipoGestion = mg.idTipoGestion_FK
inner join datos_nomina dn on dn.idDatosNomina = m.idDatosNominaRecibe_FK
inner join site si on si.idSite = dn.idSite_FK
inner join cuenta_proceso_celula cpc on cpc.idCuentaProcesoCelula =
dn.idCuentaProcesoCelula_FK
inner join mac_temagestion mtg on mtg.idTemaGestion = mg.idTemaGestion_FK
join (SELECT @acum:= 0) r
where mg.error = 'si'
and si.idSite in (2)
and cpc.idCuenta_FK in (2)
and cpc.idProceso_FK in (25)
and cpc.idCelula_FK in (12)
and tg.idTipoGestion in (1)
group by mtg.detTemaGestion
order by count(mtg.idTemaGestion) desc
) c
WHERE c.acumulado/total <= 0.80;
DROP TABLE totalParaCalcular;
DROP TABLE totalParaCalcularConAcumulado;