Oracle's row-to-column and column-changing careers

Source: Internet
Author: User

1. Row to column:

First, the initial data:

Data after conversion:

Second, the conversion of the statement:

--statistics on the number of staff in various positions in various departments:
SELECT T.job, SUM (DECODE (t.job, ' clerk ', 1, NULL)) as COUNT1
, SUM (DECODE (T.job, ' salesman ', 1, NULL)) as COUNT2
, SUM (DECODE (t.job, ' president ', 1, NULL)) as COUNT3
, SUM (DECODE (t.job, ' MANAGER ', 1, NULL)) as COUNT4
, SUM (DECODE (t.job, ' ANALYST ', 1, NULL)) as COUNT5
From EMP T
GROUP by T.job;

The result of this writing is:

Not far from what is needed, but still not.

Third, the final wording:

--statistics on the number of staff in various positions in various departments:

SELECT T.job, (case is t.job= ' clerk ' then SUM (DECODE (t.job, ' clerk ', 1, NULL))
When t.job= ' salesman ' then SUM (DECODE (T.job, ' salesman ', 1, NULL))
When t.job= ' president ' then SUM (DECODE (t.job, ' president ', 1, NULL))
When t.job= ' manager ' then SUM (DECODE (t.job, ' manager ', 1, NULL))
ELSE SUM (DECODE (t.job, ' ANALYST ', 1, NULL)) (END) as NUM
From EMP T
GROUP by T.job

The results are as follows:

Summary: Row to column is mainly used by the group BY and aggregate function (max,sum), decode function to achieve, this is the original notation. There is also a simpler way to implement transformations using pivot.

Oracle's row-to-column and column-changing careers

Related Article

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.