Oracle的decode的妙用

2021-04-02 00:49:56 字數 687 閱讀 1707

最近做了乙個報表查詢,需要從資料庫中獲取某個**商某乙個月以來的每天的供貨記錄.

由於表設計的時候每一條供貨記錄對應資料庫中的一條記錄,所以存在乙個行轉列的問題,

這時decode函式的功能就能得一體現了.

我想了個比較傻的辦法,

sum(decode(to_char(t.sami_samplelingtime,'dd'),'01',tt.saca_numberofbarrels,0)) as sast_1

sum(decode(to_char(t.sami_samplelingtime,'dd'),'02',tt.saca_numberofbarrels,0)) as sast_2

sum(decode(to_char(t.sami_samplelingtime,'dd'),'03',tt.saca_numberofbarrels,0)) as sast_3

......

這樣的話就可以將某一天的資料統計出來轉換成列的形式.

decode(to_char(t.sami_samplelingtime,'dd') --- 比對資訊中包含的日期資訊

,'01'----要比對的日期,tt.saca_numberofbarrels----要獲取的列值,0----列值為空的時候的備選值)

呵呵 這樣就可以用一句sql語句查詢出來,乙個月的每天的資訊.

寫在著僅為以後自己查詢方便呵呵

oracle中的decode函式

decode函式的用法解釋 1 decode 條件,a,b,c 相當於 if 條件 a then b else c 2 decode 條件,a,b,c d,e,f,g 相當於 if 條件 a then b else if 條件 c then d,else if 條件 e then f else g ...

oracle中的decode函式

decode函式是oracle pl sql是功能強大的函式之一,目前還只有oracle公司的sql提供了此函式,其他資料庫廠商的sql實現還沒有此功能。decode有什麼用途 呢?先構造乙個例子,假設我們想給智星職員加工資,其標準是 工資在8000元以下的將加20 工資在8000元以上的加15 通...

oracle中decode 的作用

decode函式相當於一條件語句 if 它將輸入數值與函式中的引數列表相比較,根據輸入值返回乙個對應值。函式的引數列表是由若干數值及其對應結果值組成的若干序偶形式。當然,如果未能與任何乙個實參序偶匹配成功,則函式也有預設的返回值。區別於sql的其它函式,decode函式還能識別和操作空值.其具體的語...