有两张表 许可表inf_apply和处罚表inf_punish他们都有以下字段internal_no,department,name,is_finish,accept_time分别表示 办件编号,部门,办件名称,是否完成,受理时间两张表的大体概念是:一个部门受理的办件分为许可和处罚两种状态 但是一个办件不可能在两张表中同时有 办件名称对应多个办件
比如民政局有一个办件名称叫婚姻登记,可以有多个来办理此项的办件 许可的在许可表中,不许可的在处罚表中 许可表和处罚表中的数据又可分为完成和未完成两种状态
现在我想得到一张新表包含以下字段:internal_no,本办件属于哪张表,department,name,本部门办件的总数,本部门办件的已完成数,本部门办件的未完成数
得到这个表后可以按部门和受理时间查询

解决方案 »

  1.   


    /--按部门和受理时间查询上述内容:select tab1.z 办件总数, tab2.z 已完成数, tab3.z 未完成数
      from (select t1.c + t2.c z
              from (select count(*) c
                      from inf_apply
                     where department = '本部门'
                       and to_char(accept_time, 'yyyy-mm-dd') >= '2008-10-01'
                       and to_char(accept_time, 'yyyy-mm-dd') <= '2008-10-26') t1,
                   (select count(*) c
                      from inf_punish
                     where department = '本部门'
                       and to_char(accept_time, 'yyyy-mm-dd') >= '2008-10-01'
                       and to_char(accept_time, 'yyyy-mm-dd') <= '2008-10-26') t2) tab1,   /--本部门办件的总数
           (select t1.c + t2.c z
              from (select count(*) c
                      from inf_apply
                     where department = '本部门'
                       and upper(is_finish) = 'Y'
                       and to_char(accept_time, 'yyyy-mm-dd') >= '2008-10-01'
                       and to_char(accept_time, 'yyyy-mm-dd') <= '2008-10-26') t1,
                   (select count(*) c
                      from inf_punish
                     where department = '本部门'
                       and upper(is_finish) = 'Y') and
                   to_char(accept_time, 'yyyy-mm-dd') >= '2008-10-01' and
                   to_char(accept_time, 'yyyy-mm-dd') <= '2008-10-26' t2) tab2,            /--本部门办件的未完成数
           (select t1.c + t2.c z
             from (select count(*) c
                     from inf_apply
                    where department = '本部门'
                      and upper(is_finish) = 'N'
                      and to_char(accept_time, 'yyyy-mm-dd') >= '2008-10-01'
                      and to_char(accept_time, 'yyyy-mm-dd') <= '2008-10-26') t1,
                  (select count(*) c
                     from inf_punish
                    where department = '本部门'
                      and upper(is_finish) = 'N'
                      and to_char(accept_time, 'yyyy-mm-dd') >= '2008-10-01'
                      and to_char(accept_time, 'yyyy-mm-dd') <= '2008-10-26') t2) tab3;   /--本部门办件的未完成数
           
      

  2.   

        本办件属于哪张表强烈建议在两张表使用一个表示字段吧(1,代表inf_apply  2,代表inf_punish)