ORACLE row-to-column writing/* drop table foo; * // ** CREATE a demo TABLE **/create table foo (bbbid number (2 ), -- primary key depid number (2), -- unit NUMBER AAC006 VARCHAR2 (1) -- Gender 1 male 2 female 0 unknown CONSTRAINT CK_AAC006 CHECK (AAC006 = '1' OR AAC006 = '2' OR AAC006 = '0 ')); www.2cto.com/** INSERT data for testing **/insert into foo values (, '1'); insert into foo values (, '0 '); insert into foo values (, '2'); insert into foo values (, '2'); insert into foo values (, '1'); insert into foo values (, '0'); insert into foo values (, '2'); insert into foo values, '2'); insert into foo values (, '2'); insert into foo values (, '2'); insert into foo values (, 3, '2');/** target: Perform row-to-column conversion on the table to obtain the following result set: depid male unknown 1 www.2cto.com 1 22 1 1 23 0 0 3 * // ** raw data **/SELECT * from foo;/** values are determined by gender code table, add FOO. value After AAC006 is converted to 3 columns **/select depid, DECODE (AAC006, '0',) "male", DECODE (AAC006, '1) "Female", DECODE (AAC006, '2',) "unknown" from foo; www.2cto.com/** to process in the previous result set, you will get the row-to-column Conversion Result! **/Select depid, SUM (DECODE (AAC006, '0',) "male", SUM (DECODE (AAC006, '1',) "female ", SUM (DECODE (AAC006, '2', 20120624) "unknown" from foo group by depid;/** conclusion (czw): Row-to-column conversion, in fact, it is to split a field into several fields according to the code value, and finally obtain the corresponding value through the clustering function. **/Prepared by ziwen00