小編給大家分享一下Oracle中decode怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、烏恰網(wǎng)站維護(hù)、網(wǎng)站推廣。
語法結(jié)構(gòu)如下:
decode (expression, sch_1, res_1)
decode (expression, sch_1, res_1, sch_2, res_2)
decode (expression, sch_1, res_1, sch_2, res_2, ...., sch_n, res_n)
decode (expression, sch_1, res_1, default)
decode (expression, sch_1, res_1, sch_2, res_2, default)
decode (expression, sch_1, res_1, sch_2, res_2, ...., sch_n, res_n, default)
比較表達(dá)式和搜索字,如果匹配,返回結(jié)果;如果不匹配,返回default值;如果未定義default值,則返回空值。
select name,sub,decode(sub, 'chinese',score,0) from student_score;
decode函數(shù)的功能在于它不但可以判斷常值,同樣可以判斷字段.以上語句:
當(dāng)科目為chinese時(shí),選擇score字段對(duì)應(yīng)的值,而非chinese科目,其對(duì)應(yīng)的score值都為0
select name,
sum(decode(subject, 'Chinese', nvl(score, 0), 0)) "Ch",
sum(decode(subject, 'Mathematics', nvl(score, 0), 0)) "Math",
sum(decode(subject, 'English', nvl(score, 0), 0)) "En"
from xxx group by name; ---行轉(zhuǎn)列
decode函數(shù)同樣可以當(dāng)做條件使用,如: where score= decode(subject, 'Chinese',score)
等同于以下case when
select name,
sum(case when subject='Ch'
then nvl(score,0)
else 0
end) "Ch",
sum(case when subject='Math'
then nvl(score,0)
else 0
end) "Math",
sum(case when subject='En'
then nvl(score,0)
else 0
end) "En"
from xxx group by name;
看完了這篇文章,相信你對(duì)“Oracle中decode怎么用”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!