有子查询加入id的子句的错误1054分组

时间:2013-04-11 15:03:53

标签: mysql sql mysql-error-1054 mysql-5.5

得到了这个问题:

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(按期间排序),以查看它们是否有大于或小于百分比的差异(在本例中为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(在this链接上获取)

为什么我收到了这个错误?我认为我可以在子组之后加入having子句中的子查询(就像where子句一样)

或者我错过了一个错误?

提前致谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

如果它不是,我认为优化器可能会变得混乱,因为having子句具有组中某些内容的信息......"在()中有P1.periodo&#34 ;。 P1.periodo也是一个小组。

据我了解,group by适用于不是聚合的字段。哪些字段不是聚合。

拥有的是那个......那个"基于你拥有的东西来过滤">您还没有聚合P1.periodo,但如果这是错误消息有点落后的问题。

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

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

Tuff告诉没有与DB的实际连接但可能是这样的。另外,我会将所有别名,因为它可以告诉哪些列在哪些表中...

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 
    .....