得到这个查询:
select
T.id_empresa, max(periodo) as ultimoPeriodo, (
select sum(monto) from cuotasindical.pagos
where id_empresa = T.id_empresa and periodo = max(T.periodo)
) as ultimoMonto,
min(periodo) as periodoAnterior, (
select sum(monto) from cuotasindical.pagos
where id_empresa = T.id_empresa and periodo = min(T.periodo)
) as anteriorMonto
from
(
select
P1.id_empresa, P1.periodo, sum(monto) as monto
from cuotasindical.pagos P1
where P1.id_empresa in (select id_empresa from cuotasindical.empresa where id_delegacion = 5)
group by P1.id_empresa, P1.periodo
having P1.periodo in (
select * from (
select periodo
from cuotasindical.pagos
where P1.id_empresa = id_empresa order by periodo desc limit 2
) as L
)
and count(distinct P1.periodo) > 1
) T
group by id_empresa
having (abs(max(monto) - min(monto))*100/
(
select sum(monto) from cuotasindical.pagos T2
where T2.id_empresa = id_empresa and T2.periodo = max(periodo)
) > 10);
需要比较,对于每个 empresa,最后两个 pagos(按 periodo 排序),看看它们的差异是否大于或小于百分比(在本例中为 10)
我越来越
错误代码:1054。“where 子句”中的未知列“P1.id_empresa”
唯一具有该比较的 where 子句是具有限制的子查询
having P1.periodo in (
select * from (
select periodo
from cuotasindical.pagos
where P1.id_empresa = id_empresa
order by periodo desc limit 2
) as L
)
and count(distinct P1.periodo) > 1
双子查询是为了避免 IN 子句中的限制 2(在此链接上获取)
为什么我会收到那个错误?我认为我可以在具有子句的子查询中加入 group by 之后(就像 where 子句)
还是我遗漏了一个错误?
提前致谢