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