The demand is to count the sum of the salaries of different jobs in each department.
Sql> select Deptno,ename,job,sal from emp;
DEPTNO ename JOB SAL
---------- ---------- --------- ----------
SMITH Clerk 800
ALLEN salesman 1600
WARD salesman 1250
JONES MANAGER 2975
MARTIN salesman 1250
BLAKE MANAGER 2850
CLARK MANAGER 2450
SCOTT ANALYST 3000
Ten KING President 5000
TURNER salesman 1500
ADAMS Clerk 1100
JAMES Clerk 950
FORD ANALYST 3000
MILLER Clerk 1300
14 rows have been selected.
Sql> Select Deptno,
NVL (SUM (Decode (Job, ' MANAGER ', Sal)), 0) "S_manager",
NVL (SUM (Decode (Job, ' ANALYST ', Sal)), 0) "S_analyst",
NVL (SUM (Decode (Job, ' clerk ', Sal)), 0) "S_clerk",
NVL (SUM (Decode (Job, ' president ', Sal)), 0) "S_president",
NVL (SUM (Decode (Job, ' salesman ', Sal)), 0) "S_salesman"
From EMP
Group BY Deptno;
DEPTNO s_manager s_analyst S_clerk s_president S_salesman
---------- ---------- ---------- ---------- ----------- ----------
30 2850 0 950 0 5600
20 2975 6000 1900 0 0
10 2450 0 1300 5000 0
Sql> Select Deptno,
NVL (sum (case job when ' MANAGER ' then Sal else null end), 0) "S_manager",
NVL (sum (case job if ' ANALYST ' then Sal else null end), 0) "S_analyst",
NVL (sum (case job if ' clerk ' then Sal else null end), 0) "S_clerk",
NVL (sum (case job if ' president ' then Sal else null end), 0) "S_president",
NVL (sum (case job if ' salesman ' then Sal else null end), 0) "S_salesman"
From EMP
Group BY Deptno;
DEPTNO s_manager s_analyst S_clerk s_president S_salesman
---------- ---------- ---------- ---------- ----------- ----------
30 2850 0 950 0 5600
20 2975 6000 1900 0 0
10 2450 0 1300 5000 0
Oracle's simple career line