0

得到这个查询:

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 子句)

还是我遗漏了一个错误?

提前致谢

4

2 回答 2

2

问题是 mysql 在子查询中只有一个深层次的范围。

我也有子查询限制的问题,所以,我所做的是

    ..having P1.periodo in 
    (
       select max(periodo) as periodo
       from cuotasindical.pagos H
       where P1.id_empresa = id_empresa 
    ) OR
    P1.periodo in (
       select max(periodo) as periodo
       from cuotasindical.pagos H
       where P1.id_empresa = id_empresa and periodo != P1.periodo
    )
and count(distinct P1.periodo) > 1
于 2013-04-11T15:55:35.127 回答
0

关于特定错误,id_empresa 实际上是 cuotasindical.pagos 中的一列吗?

如果不是,我认为优化器可能会感到困惑,因为having子句通过...“在()中具有P1.periodo”具有组中某些内容的信息。P1.periodo也是一个group by。

据我了解,分组依据是针对非聚合的字段。where 用于非聚合的字段。

有一个奇怪的......“根据你聚合的内容过滤”>你没有聚合 P1.periodo,但如果这是错误消息有点欺骗的问题。

如果您的数据库支持,我会考虑调试和查看 CTE(公用表表达式)。(不确定最新的 MySQL)。

如果它不支持它,请尝试使用视图...您有相当多的代码都引用了相同的两个表。也许生成一个数据集然后根据需要连接它是一个好主意。我认为这会使事后过滤更容易。

凝灰岩告诉没有与数据库的实际连接,但可能是这样的。另外,我会为所有内容设置别名,因为它很难告诉哪些列在哪些表中......

with temp as ( 
/* Setup common table expression with basically all the info we need If your
 * DBMS doesn't support common table expressions (CTE's) then put this in a
 * view, or a temp table? */  
select 
    t1.id_empresa    as id_empresa    , 
    t1.periodo       as periodo       , 
    t2.id_delegacion as id_delegacion ,
    t2.monto         as monto
from 
    cuotasindical.pagos   as t1 inner join 
    cuotasindical.empresa as t2 on t1.id_empresa = t2.id_empresa and t2.id_delegacion = 5 
)
select 
    t1.id_empresa           as id_empresa   ,
    max(t1.ultimoPeriodo)   as ultimoPeriodo,
    min(t2.anteriorPeriodo) as anteriorPeriodo,
    sum(t3.ultimoMonto  )   as ultimoMonto , 
    sum(t4.anteriorMonto)   as anteriorMonto 
from 
    (select id_empresa, max(periodo)        as ultimoPeriodo   from temp) as t1 inner join 
    (select id_empresa, min(periodo)        as anteriorPeriodo from temp) as t2 on t1.id_empresa = t2.id_empresa inner join 
    (select id_empresa, periodo, sum(monto) as ultimoMonto     from temp) as t3 on t1.id_empresa = t3.id_empresa and t1.ultimoPeriodo = t3.periodo  inner join  
    (select id_empresa, periodo, sum(monto) as anteriorMonto   from temp) as t4 on t1.id_empresa = t4.id_empresa and t2.anteriorMonto = t4.periodo  inner join  
where 
    .....
group by 
    t1.id_empresa           
having 
    ..... 
于 2013-04-11T15:56:40.317 回答