Statement of ORACLE row-to-column Conversion

Source: Internet
Author: User

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

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.