oracle select decode判斷 ,sign使用例子

 

比較大小函數SIGN函數

sign(x)或者Sign(x)叫作符號函數,其功能是取某個數的符號(正或負):當x>0,sign(x)=1;當x=0,sign(x)=0;當x<0, sign(x)=-1;code

x能夠是函數或計算表達式原理

 

有學生成績表student,如今要用decode函數實現如下幾個功能:成績>85,顯示優秀;>70顯示良好;>60及格;不然是不及格。date

假設student的編號爲id,成績爲score,那麼:
select id, decode(sign(score-85),1,'優秀',0,'優秀',-1, 
decode(sign(score-70),1,'良好',0,'良好',-1, 
decode(sign(score-60),1,'及格',0,'及格',-1,'不及格'))) 
from student;select

decode 和 case when 使用原理都是同樣的co

 

 

SELECT  
             decode( sign(AllPerson.ID_-smallpct.HR_EFF_PPLAN_CYCLE_ID_),0,
             (AllPerson.AllPersonCount-smallpct.smallpctCount),
             AllPerson.AllPersonCount) AS noToReportNum
       
              from
           ( SELECT COUNT( pcpt1.HR_EFF_PPLAN_CYCLE_ID_) smallpctCount, pcpt1.HR_EFF_PPLAN_CYCLE_ID_  FROM
            HR_EFF_PPLAN_CYCLE cycle1,hr_eff_plan ep1 , HR_EFF_PFMCE_PLAN pp1,
            HR_EFF_PFMCE_PLANEECPT pcpt1 
            WHERE 
            ep1.STATUS_='0' AND cycle1.STATUS_='0' AND pp1.STATUS_='0' AND  pcpt1.STATUS_='0' 
             AND pcpt1.HR_EFF_PFMCE_PLAN_ID_=pp1.ID_ 
            AND pcpt1.HR_EFF_PPLAN_CYCLE_ID_=cycle1.ID_
             and to_char(sysdate, 'yy-MM')>=to_char(cycle1.OBJ_REPORT_STIME_,'yy-MM')
            AND cycle1.HR_EFF_PFMCE_PLAN_ID_=pp1.ID_ 
            AND pp1.HR_EFF_PLAN_ID_=ep1.ID_  GROUP BY pcpt1.HR_EFF_PPLAN_CYCLE_ID_ ) smallpct
        ,
        ( SELECT COUNT(news1.ID_) AllPersonCount ,cycle1.ID_  FROM
            HR_EFF_PPLAN_CYCLE cycle1,hr_eff_plan ep1 , hr_eff_news news1,HR_EFF_PFMCE_PLAN pp1
            WHERE 
            ep1.STATUS_='0' AND cycle1.STATUS_='0' AND pp1.STATUS_='0' 
            
             and to_char(sysdate, 'yy-MM')>=to_char(cycle1.OBJ_REPORT_STIME_,'yy-MM')
            AND cycle1.HR_EFF_PFMCE_PLAN_ID_=pp1.ID_ 
            AND pp1.HR_EFF_PLAN_ID_=ep1.ID_
            AND news1.HR_EFF_PLAN_ID_=ep1.ID_ GROUP BY cycle1.ID_ ) AllPerson
       cas

 

 

SELECT decode ( (COUNT(1)),0,2,(COUNT(1)) )  from      ( SELECT COUNT( pcpt1.HR_EFF_PPLAN_CYCLE_ID_) smallpctCount, pcpt1.HR_EFF_PPLAN_CYCLE_ID_  FROM
            HR_EFF_PPLAN_CYCLE cycle1,hr_eff_plan ep1 , HR_EFF_PFMCE_PLAN pp1,
            HR_EFF_PFMCE_PLANEECPT pcpt1 
            WHERE 
            ep1.STATUS_='0' AND cycle1.STATUS_='0' AND pp1.STATUS_='0' AND  pcpt1.STATUS_='0' 
             AND pcpt1.HR_EFF_PFMCE_PLAN_ID_=pp1.ID_ 
            AND pcpt1.HR_EFF_PPLAN_CYCLE_ID_=cycle1.ID_
             and to_char(sysdate, 'yy-MM')>=to_char(cycle1.OBJ_REPORT_STIME_,'yy-MM')
            AND cycle1.HR_EFF_PFMCE_PLAN_ID_=pp1.ID_ 
            AND pp1.HR_EFF_PLAN_ID_=ep1.ID_  GROUP BY pcpt1.HR_EFF_PPLAN_CYCLE_ID_ ) hasReport 生成

相關文章
相關標籤/搜索