select * from (Select count(*) as count1  from edr_cont_hold_release Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=1 )
union  ( Select count(*)  as count1  from edr_cont_hold_release Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=2 and  ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >1)
 union ( Select count(*) as count1 from edr_cont_hold_release Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=3 and ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >2)
 union  ( Select  count(*) as count1 from edr_cont_hold_release Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >3 )
这个sql语句如何优化哇,请高手作答,谢谢

解决方案 »

  1.   


    /*
    1.请用sql代码发
    2.排版请美观
      

  2.   

    TO_NUMBER(release_datetime - cont_in_tml_datetime))
    这是个自定义函数?
      

  3.   

    你这个是四个不同条件的数量,没什么可优化的,不过最外面一层的嵌套没什么用处.
    直接这样即可.
    Select count(*) as count1 from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=1 
    union 
    Select count(*) as count1 from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=2 and ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >1
    union
    Select count(*) as count1 from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=3 and ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >2
    union
    Select count(*) as count1 from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >3 
      

  4.   

    建议把union 换成union all,如果你不想排序,可加个参数,然后order by 
    以下两段,自己试试哪段是正确的.Select count(*) as count1 , 1 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=1 
    union 
    Select count(*) as count1 , 2 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=2 and ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >1
    union
    Select count(*) as count1 , 3 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=3 and ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >2
    union
    Select count(*) as count1 , 4 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >3 
    order by px或者select * from 
    (
    Select count(*) as count1 , 1 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=1 
    union 
    Select count(*) as count1 , 2 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=2 and ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >1
    union
    Select count(*) as count1 , 3 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) <=3 and ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >2
    union
    Select count(*) as count1 , 4 px from edr_cont_hold_release 
    Where cont_status in ('IF','EM') And HOLD_CONDITION = 'DT' And ROUND(TO_NUMBER(release_datetime - cont_in_tml_datetime)) >3 
    ) t
    order by px