oracle 行列轉換,oracle行列

來源:互聯網
上載者:User

oracle 行列轉換,oracle行列

實際情況:工作中遇到這種表格,需要查詢到每個key='key'(即費用編碼)對應的key='value'(即減免金額)的值

因此就需要用到行列轉換,方便查詢

需要的查詢結果如下

sql語句如下(兩種方式):

(1)利用oracle中的decode函數

1 select t.index_id,2        MAX(decode(t.key, 'key', t.value, null)) as fee_code,3        MAX(decode(t.key, 'value', t.value, null)) as reduction_fee,4        MAX(decode(t.key, 'devalue', t.value, null)) as overdue_payperiod5   from LA_T_REDUCTION_REGULAR t6  where t.contract_id = '20171205D0904J9C0_18'7  group by t.index_id8  order by t.index_id

(2)使用case-when-then-else

 1 SELECT t.index_id, 2        MAX(CASE 3              WHEN t.key = 'key' THEN 4               t.value 5              ELSE 6               null 7            END) fee_code, 8        MAX(CASE 9              WHEN t.key = 'value' THEN10               t.value11              ELSE12               null13            END) reduction_fee,14        MAX(CASE15              WHEN t.key = 'devalue' THEN16               t.value17              ELSE18               null19            END) overdue_payperiod20   FROM LA_T_REDUCTION_REGULAR t21  where t.contract_id = '20171205D0904J9C0_18'22  GROUP BY t.index_id23  order by t.index_id

相比較而言,使用decode函數sql語句顯得更為簡潔,但二者實際的內部邏輯是相同的,而且decode函數是oracle特有的。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.