两张表,1.flowcase: 有三个字段,case_seq,oper_name,flowtype_id
       2.flowtype: 有三个字段,flowtype_id,flowtype_name,parent_fid解释下:可以把它看成是政府信访办接待群众信访的场景,有人来信访办办事,接待人员每受理一件事,就向flowcase表写一条记录,case_seq是流水号,oper_name是信访办的接待人员,flowtype_id是所办事项id。
flowtype表存放的是关于事项的信息。事项分为几大类,如sx1,sx2,sx3 等,每一类下又包含很多实际可办理的事项,如大类sx1 下包含 sx1-1、sx1-2,sx1-3..., sx2 下包含sx2-1、sx2-2等。flowtype_id 事项id,flowtype_name 事项名称,parent_fid 事项所属类别
flowcase表中存放的数据格式如下:
...
2022  张三   sx2-1
2023  张三   sx1-4
2024  李四   sx4-3
2025  李四   sx3-8
...
flowtype表中存放的数据格式如下:
...
sx2-1  事项2-1  sx2
sx2-2  事项2-2  sx2
...
sx5-1  事项5-1  sx5  
...我的要求是:查询出信访办每一个接待人员所办理的事项大类的数量。
即:张三sx1类事项办了多少件,sx2类办了多少件,李四sx5类的事项办了多少件等等。
我草写了一个sql语句,可是不是我要的结果,拿出来大家参考下:
select t1.oper_name, --人员
        t2.parent_fid,--事项类别
        count(t1.case_seq) num--数量
  from ja_bs_flowcase t1, ja_bs_flowtype t2
 where t2.flowtype_id = t1.flowtype_id
 group by t1.oper_name, t2.parent_fid查询结果:oper_name  parent_fid  num
            张三         sx2      10
              李四         sx5       46
            李四        sx6      18
            ......
问题:上面结果它只是查出了实际那个员工办了多少件某类型事项,某一类型事项如果这个人没有办,则查询结果中不会出现记录。
可是我想要的恰恰是这个,我希望只要这个员工办理了事项,那么就列出所有事项类型的办理数量(某一个人的),即使没有办理过该类型事项,也要列出来,只是数量为0 
假定事项类型有3大类:sx1,sx2,sx3
我希望的查询结果是:oper_name  parent_fid  num
                   张三         sx1        4
                      张三         sx2        0
                      张三         sx3        23
                   李四         sx1        7
                   李四         sx2        11
                   李四         sx3        0
                   .......说了这么多,有点汗,希望我把问题说清楚了,还望各位高手不吝赐教。

解决方案 »

  1.   

    问题一:
    select
    oper_name,parent_fid,count(flowtype_id)
    from
    flowcase,flowtype
    where
    flowcase.flowtype_id=flowtype.flowtype_id
    group by oper_name,parent_fid问题二
    select
    oper_name,parent_fid,count(flowtype_id)
    from
    flowcase,flowtype
    where
    flowcase.flowtype_id=flowtype.flowtype_id(+)
    group by oper_name,parent_fid
      

  2.   

    挣扎求存(隼) 朋友,你的sql也不是我的要求,结果跟我草写的那个sql查询的结果一样,也是只有实际的受理量,而不是每个人对所有类型的受理量。不过还是谢谢
      

  3.   

    SELECT 
           ( CASE WHEN parent_fid = 'sx1' THEN 'sx1‘  WHEN parent_fid = 'sx2' THEN 'sx2'     ELSE 'sx3' END ) 案件类型,
           SUM( DECODE(接待人,'张三',数量,0) )张三, 
           SUM( DECODE(接待人,'李四',数量,0) ) 李四,
       SUM(数量) 合计
    FROM(SELECT   oper_name 接待人, parent_fid 案件类型, count(case_seq) 数量
         FROM (SELECT oper_name, parent_fid, 
                 FROM  ja_bs_flowcase)
     GROUP BY parent_fid, oper_name)
      
    GROUP BY  parent_fid ORDER BY 案件类型
    没有经过测试给你个别人以前写给我的脚本稍微改动了一下不知道对你有没有用
      

  4.   

    create table flowcase(case_seq varchar2(100),oper_name varchar2(100),flowtype_id varchar2(100))
    create table flowtype(flowtype_id varchar2(100),flowtype_name varchar2(100),parent_fid varchar2(100))
    insert into flowcase values('2022','张三','sx2-1');
    insert into flowcase values('2023','张三','sx1-4');
    insert into flowcase values('2021','张三','sx1-1');
    insert into flowcase values('2024','李四','sx4-3');
    insert into flowcase values('2025','李四','sx3-8');
    insert into flowtype values ('sx1-1', '事项1-1', 'sx1')
    insert into flowtype values ('sx1-4', '事项1-4', 'sx1')
    insert into flowtype values ('sx2-1', '事项2-1', 'sx2')
    insert into flowtype values ('sx2-2', '事项2-2', 'sx2');
    insert into flowtype values ('sx5-1', '事项5-1', 'sx5');
    insert into flowtype values ('sx4-3', '事项4-3', 'sx4');
    insert into flowtype values ('sx3-8', '事项3-8', 'sx3');select t.oper_name, t1.parent_fid, count(1) num
      from flowcase t, flowtype t1
     where t.flowtype_id = t1.flowtype_id
     group by t.oper_name, t1.parent_fid
    union
    select t.oper_name,
           t1.parent_fid,
           case
             when count(1) > 0 then
              0
             else
              0
           end num
      from flowcase t, flowtype t1
     where not exists (select 1
              from flowcase f1, flowtype f2
             where f1.flowtype_id = f2.flowtype_id
               and t.oper_name = f1.oper_name
               and t1.parent_fid = f2.parent_fid)
     group by t.oper_name, t1.parent_fid
     order by oper_name,parent_fid
    --运行结果
        OPER_NAME PARENT_FID NUM
    1 李四 sx1 0
    2 李四 sx2 0
    3 李四 sx3 1
    4 李四 sx4 1
    5 李四 sx5 0
    6 张三 sx1 2
    7 张三 sx2 1
    8 张三 sx3 0
    9 张三 sx4 0
    10 张三 sx5 0
    测试了下,觉的行.不知道有没更好的方法,期待高手出现,呵
      

  5.   

    机房没有oracle的环境,只好用mysql测试一下。
    用的icss_zhen的测试数据,SQL如下:select t.oper_name,t.parent_fid,ifnull(t2.num,0) num from 
    (select t1.oper_name,
    t2.parent_fid
    from flowcase t1, flowtype t2
    group by t1.oper_name, t2.parent_fid) t
    left join 
    (select t1.oper_name,
    t2.parent_fid,
    count(t1.case_seq) num
    from flowcase t1, flowtype t2 
    where t2.flowtype_id = t1.flowtype_id 
    group by t1.oper_name, t2.parent_fid
    ) t2
    on t.oper_name=t2.oper_name and t.parent_fid=t2.parent_fid;能达到效果,感觉应该有简单的方法实现,期待高手。
      

  6.   

    在db2中value(count(1),0)可以将统计结果为空时,变成值为0的记录。
    看到terry850730中用到了ifnull(t2.num,0) 应该和value(count(1),0)的作用差不多,
    感觉这样可以:
         select t1.oper_name, --人员 
            t2.parent_fid,--事项类别 
            ifnull(count(t1.case_seq),0) num--数量 
         from ja_bs_flowcase t1, ja_bs_flowtype t2 
        where t2.flowtype_id = t1.flowtype_id 
        group by t1.oper_name, t2.parent_fid 
        order by t1.oper_name, t2.parent_fid 我这里没有环境,不能测试。
      

  7.   

    这个需求不难, 这样就可以了select a.oper_name, b.parent_fid, nvl(c.num,0) as num
    from
    (select distinct oper_name from ja_bs_flowcase) a,
    (select distinct parent_fid from ja_bs_flowtype) b
    (
    select t1.oper_name, --人员
            t2.parent_fid,--事项类别
            count(t1.case_seq) num--数量
      from ja_bs_flowcase t1, ja_bs_flowtype t2
    where t2.flowtype_id = t1.flowtype_id
    group by t1.oper_name, t2.parent_fid
    ) c
    where a.oper_name=c.oper_name(+)
    and b.parent_fid=c.parent_fid(+)
    /
      

  8.   

    distinct 能不用做好还是不用,效率超低