有表A,B,C
表A结构如下 公司id,行业
表B结构如下 参保人id,公司id
表c结构如下 外来人员id,公司id
现在需要显示如下结构
行业 公司数量 参保人员数量 外来人员数量
总数
请各位大侠帮帮忙,谢谢
表A结构如下 公司id,行业
表B结构如下 参保人id,公司id
表c结构如下 外来人员id,公司id
现在需要显示如下结构
行业 公司数量 参保人员数量 外来人员数量
总数
请各位大侠帮帮忙,谢谢
s.行业,
count(s.*) as 公司数量,
sum(s.参保人员) as 参保人员数量,
sum(s.外来人员) as 外来人员数量
from
(select
t.公司id,
t.行业,
(select count(*) from 表B where 公司id=t.公司id) as 参保人员,
(select count(*) from 表C where 公司id=t.公司id) as 外来人员,
from
表A t) s
group by
s.行业
sql 条件直等好像有些漏洞,献丑了!
-------------------->
select
t1.行业,count(t1.公司id) as 公司数量,
decode(t2.参保人员数量,null,0),
decode(t3.外来人员数量,null,0)
from 表A t1,
(select 公司id,count(*) as 参保人员数量 from 表B group by 公司id ) t2,
(select 公司id,count(*) as 外来人员数量 from 表C group by 公司id ) t3where
t1.公司id=t2.公司id(+)
and t1.公司id=t2.公司id(+)
and t1.公司id=t3.公司id(+)
group by
t1.行业
from hangye a,
(select gongsi_id,count(cbry_id)cbry from gongsi
group by gongsi_id) b,
(select gongsi_id,count(wlry_id)wlry from renyuan
group by gongsi_id) c
where a.gongsi_id=b.gongsi_id(+)
and a.gongsi_id =c.gongsi_id(+)
group by a.hangye_id