我想要做的是计算子查询返回的行数,基本上如下:
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
谢谢!
答案 0 :(得分: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