DB2 row to column (multidimensional)

Source: Internet
Author: User
Tags db2

Multi-dimensional conversion of row and column, the following row and column conversion, according to the customer, the Bank of the organization of the row.

-----------------Building a tableCREATE TABLECust_bank_info (cust_idVARCHAR(Ten), Cust_nameVARCHAR( -), Bank_noVARCHAR( -), BANKVARCHAR( -), Money_type_noVARCHAR( -), Money_typeVARCHAR( -),  Money INT);INSERT  intoCust_bank_infoVALUES('C001','Yun Yin','B01','Bank of China','MT01','Banking',10000);INSERT  intoCust_bank_infoVALUES('C002','John Doe','B01','Bank of China','MT02','Loans',20390);INSERT  intoCust_bank_infoVALUES('C003','Harry','B01','Bank of China','MT03','Deposit',29301);INSERT  intoCust_bank_infoVALUES('C004','Chen Liu','B01','Bank of China','MT04','Education Savings',10000);INSERT  intoCust_bank_infoVALUES('C001','Yun Yin','B02','Bank of China','MT01','Banking',20000);INSERT  intoCust_bank_infoVALUES('C005','JACK','B02','ICBC','MT05','Banking',10001);INSERT  intoCust_bank_infoVALUES('C006','Wang San','B02','ICBC','MT06','Loans',10002);INSERT  intoCust_bank_infoVALUES('C007','Liu Liu','B02','ICBC','MT07','Deposit',10003);INSERT  intoCust_bank_infoVALUES('C008','Zheng Qi','B02','ICBC','MT08','Education Savings',10004);
SELECT *  from Cust_bank_info
The query results are as follows:

--DELETE from Cust_bank_infoDROP TABLEMapping;CREATE TABLEMapping (Cn_nameVARCHAR( -), code_lVARCHAR(Ten), Code_nVARCHAR(Ten))INSERT  intoMappingValues('Banking','MT01','bank_01');INSERT  intoMappingValues('Banking','MT05','bank_01');INSERT  intoMappingValues('Loans','MT02','bank_02');INSERT  intoMappingValues('Loans','MT06','bank_02');INSERT  intoMappingValues('Deposit','MT03','bank_03');INSERT  intoMappingValues('Deposit','MT07','bank_03');INSERT  intoMappingValues('Education Savings','MT04','bank_04');INSERT  intoMappingValues('Education Savings','MT08','bank_04'); SELECTcust_id, Cust_name, Bank_no, BANK,Max( Case  whenB.code_n='bank_01'  Then  Money ELSE '0' END) asFinancial,Max( Case  whenB.code_n='bank_02'  Then  Money ELSE '0' END) asloans,Max( Case  whenB.code_n='bank_03'  Then  Money ELSE '0' END) asDeposit,Max( Case  whenB.code_n='bank_04'  Then  Money ELSE '0' END) asEducation Savings fromCust_bank_info A Left JOINMAPPING B onA.money_type_no=b.code_lGroup  byCust_id,cust_name,bank,bank_no

The query results are as follows:

DB2 row to column (multidimensional)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.