Re-write row-to-column and column-changing careers

Source: Internet
Author: User

Original table:

1. A career change is to turn a column of data into a row display, and a column switch uses the sum aggregation function, and when the case is judged, the column change needs to define its own alias.

Column Change statement:

SELECT SUM (case
When t.loc = ' NEW YORK ' then
T.deptno
END) as column_00001,
SUM (case
When t.loc = ' DALLAS ' Then
T.deptno
END) as column_00002,
SUM (case
When t.loc = ' CHICAGO ' Then
T.deptno
END) as column_00003,
SUM (case
When t.loc = ' BOSTON ' Then
T.deptno
END) as column_00004
From DEPT T; --Career change

The results displayed:

2. Row to column is a row to make a column display, row to column uses the SUM aggregate function, when the case is judged, the alias is the same as the value of each column in each row.

Row to Column code:

SELECT SUM (case
When t.loc = ' NEW YORK ' then
T.deptno
END) as NewYork,
SUM (case
When t.loc = ' DALLAS ' Then
T.deptno
END) as DALLAS,
SUM (case
When t.loc= ' CHICAGO ' then
T.deptno
END) as CHICAGO,
SUM (case
When t.loc= ' BOSTON ' then
T.deptno
END) as BOSTON
From DEPT T;

Show Results:

Re-write row-to-column and column-changing careers

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.