oracle 统计

来源:百度知道 编辑:UC知道 时间:2024/05/28 19:30:55
办公室表office和人员表users表通过officeid关联,现在统计每个办公室有多少人
select officename,count(*) from office o,users u where o.officeid=u.officeid group by officename
现在数据只统计了有人数的办公室人数,我想把没有人数的办公室也列出来,统计数为0
高手帮帮我

select o.officename,count(u.*)
from office o,users u
where o.officeid=u.officeid(+)
group by o.officename

用左联或右联么..试试吧.
加号不是在右边o.officeid=u.officeid(+)
就是在左边o.officeid(+)=u.officeid..

select o.*,(select count(*) from users u where u.officeid=o.officeid) as num from office o;

你的office表在前,根据你的要求应该使用左外连接。

select officename,nvl(count(1),0) as cnt from office o,users u where o.officeid=u.officeid(+) group by officename