3

我想要做的是计算子查询返回的行数,基本上如下:

select pp.prop_id, COUNT((select employee_id from employee e where e.ao1_hours > 0))
  from proposal_piece pp

  group by pp.prop_id
  order by pp.prop_id

这是我的错误信息:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

为什么这不起作用?如果 select 只是返回一堆employee_id's带有过滤条件的,为什么我不能计算行数或employee_id's返回的行数?

我正在计算拥有ao1_hours > 0. 按 分组prop

这是关于我的数据库的一些结构信息,作为查询的一部分。

    from proposal_piece pp
    INNER JOIN employee e
    on pp.employee_id = e.employee_id

谢谢!

4

1 回答 1

5

试试这个

select pp.prop_id, 
      (select COUNT(employee_id) 
       from employee e 
       where e.ao1_hours > 0 and e.employee_id = pp.employee_id) as nb_employees
from proposal_piece pp      
order by pp.prop_id   

或这个

select pp.prop_id, count(e.employee_id) as nb_employees  
from proposal_piece pp inner join employee e 
     on pp.employee_id = e.employee_id
where e.ao1_hours > 0
group by pp.prop_id
order by pp.prop_id
于 2012-07-03T13:52:54.820 回答