有一个表:t_mp_problem_accept,里面有一个状态字段
该表有很复杂的WHERE条件,
在执行查询后的基础上,分别统计
已办结数 已办结率 逾期未办结数 逾期未办结率
这些都是在刚刚查询的视图里再次根据状态字段进行统计.
SQL语句的构想如下面所示:select c.*
,(select count(*) from c where c.state_id='a')
,(select count(*) from c where c.state_id='b')
,(select count(*) from c where c.state_id='c')
,(select count(*) from c where c.state_id='d')from
(select a.accept_id,a.accept_dept,a.accept_man,a.accept_speciality,
a.deliver_speciality_id,a.flow_id,a.problem_id,a.state_id 
from t_mp_problem_accept a where ...
) c
但这个SQL语句报“表和视图不存在”,意思是
select count(*) from c where c.state_id='a'里面的表C不存在.
我应该怎么设计SQL呢?有没有好的方法?

解决方案 »

  1.   

    select c.accept_id
    ,sum(decode(state_id,'a',1,0)),
    sum(decode(state_id,'b',1,0)),
    sum(decode(state_id,'c',1,0)),
    sum(decode(state_id,'d',1,0))
    from
    (select a.accept_id,a.accept_dept,a.accept_man,a.accept_speciality,
    a.deliver_speciality_id,a.flow_id,a.problem_id,a.state_id 
    from t_mp_problem_accept a where ...
    ) c
    group by accept_id
      

  2.   

    我理解没错的话,就是1楼写的sql楼主也不出来说句话
      

  3.   

    我觉得可以用用UION试试,
    每个情况后面加不同的条件,然后把所有结果联合UION一下.