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