需求是统计当前公司下的用户总数:公司表tbl_organization,用户表tbl_users,公司表的id为用户表的org_id,现需要展现所有公司下的用户总数,所以感觉需要写一个存储过程重新建一个表来存储对应公司下的用户总数,但没有写过存储过程,特请各位朋友帮忙写一个,谢谢了
这是一个根据公司id查询该公司下用户总数的sql语句,供各位朋友参考:select count(*) from tbl_users u where u.id>0 and u.org_id in (
select org.id from tbl_organization org where org.deleted=0
start with org.id in (20776) connect by nocycle prior org.id=org.org_parent_id )
and u.deleted=0
这是一个根据公司id查询该公司下用户总数的sql语句,供各位朋友参考:select count(*) from tbl_users u where u.id>0 and u.org_id in (
select org.id from tbl_organization org where org.deleted=0
start with org.id in (20776) connect by nocycle prior org.id=org.org_parent_id )
and u.deleted=0
组织表:用户表:这样行不?对于新建那个表我觉得主要就是得有个org_id,org_name以及统计出来的它下边的用户总数暂且用countUser好了,
组织表:用户表:这样行不?对于新建那个表我觉得主要就是得有个org_id,org_name以及统计出来的它下边的用户总数暂且用countUser好了,
is begin create table tbl_new as select a.id,a.org_name,nvl(count(b.org_id),0) countUser from tbl_organization a left join tbl_users b
on a.id=b.org_id and b.deleted=0 and a.deleted=0
group by a.id ,a.org_nameend;
begin
select count(*) from tbl_users u where u.id>0 and u.org_id in (
select org.id from tbl_organization org where org.deleted=0
start with org.id in (20776) connect by nocycle priorrg.id=org.org_parent_id ) and u.deleted=0;end proc_name;
create or replace procedure proc_name is
l_count number(5);
begin
select count(*) into l_count from tbl_users u where u.id>0 and u.org_id in (
select org.id from tbl_organization org where org.deleted=0
start with org.id in (20776) connect by nocycle priorrg.id=org.org_parent_id ) and u.deleted=0;end proc_name;
把所有子公司的id都列在一张表上就能解决递归的问题,子公司信息和用户信息放在一张表也不规范。
要是想把子公司和母公司区分开,设计成:
母公司id 子公司id
AAA aa
AAA bb
数据库操作要的是语句简单。
加上12楼的
create or replace procedure proc_name is
l_count number(5);
begin
for i in (select (取子公司ID字段) from tbl_organization) loop
select count(*) into l_count from tbl_users u where u.id>0 and u.org_id in (
select org.id from tbl_organization org where org.deleted=0
start with org.id in (i) connect by nocycle priorrg.id=org.org_parent_id ) and u.deleted=0;
end loop ;
commit;
end proc_name;
你好,不是说有固定的公司id,而是所有公司和它下边的用户数都统计下来,公司下的用户数包括它的子公司的用户数和它子公司的子公司的用户数,不是说跟我写那个sql语句一样直接输入一个公司id然后查出来一条条插,而是一下子全部的插进去,
is
begin
insert into gohigh2.tbl_zy (
id,
org_id,
org_name,
user_count
)
select gohigh2.seq_tbl_zy.nextval as id,
n.org_id,n.org_name,n.user_count from
(
select
o.id org_id,
o.org_name org_name,
nvl(count(u.org_id),0) user_count
from tbl_organization o left join tbl_users u on o.id= u.org_id and o.deleted=0 and u.deleted=0 group by o.id,o.org_name
) n
;
commit;
end proc_zy;