现在要编写一个存储过程 现在有表A结构如下
id(部门编号)  eid(员工编号) xb(性别)
执行存储过程后在表B 结构如下
id(部门编号) sum(部门人数) fm(男性人数) mm(女性人数) 存储过程主要实现根据输入的部门编号分类统计部门人数、部门男性人数、部门女性人数以及公司所有总人数请各位高手支个招 不胜感谢

解决方案 »

  1.   

    --写个语句,存储过程还是自己动动手吧
    SELECT id,
           COUNT(*) 部门人数,
           SUM(decode(xb, '男', 1, 0)) 男性人数,
           SUM(decode(xb, '女', 1, 0)) 女性人数,
      FROM a
     WHERE id = 指定部门编号
      

  2.   


    create table b(
           id varchar2(10) references a(id),--与a表id号类型相同
           sums number(10),
           fm number(10),
           mm number(10)
           )
    /
    create or replace procedure pro_show_emp(
           id_in in a.id%type
           )
    as
    begin
         insert into b
         select id,
                count(*),
                sum(decode(xb,'男',1,0)),
                sum(decode(xb,'女',1,0))
         from a
         where id=id_in;
         commit;
         
         exception
         when others then
              rollback;
    end pro_show_emp;
      

  3.   


    with t as(
    select 10 deptid,'1' empid,0 sex from dual
    union all
    select 10,'2',1 from dual
    union all
    select 20,'3',0 from dual
    union all
    select 10,'4',0 from dual
    union all
    select 30,'5',1 from dual
    union all
    select 10,'6',0 from dual
    union all
    select 40,'7',1 from dual
    union all
    select 40,'8',1 from dual
    union all
    select 30,'9',0 from dual
    )
    select deptid "部门",
           count(1) 部门人数,
           sum(decode(sex, 0, 1, 0)) "部门男性人数",
           sum(decode(sex, 0, 0, 1)) "部门女性人数",
           sum(count(1)) over() "总数"
      from t
     group by deptid
     
            部门       部门人数       部门男性人数       部门女性人数         总数
    ---------- ---------- ------------ ------------ ----------
            10          4            3            1          9
            20          1            1            0          9
            30          2            1            1          9
            40          2            0            2          9