Creating a matrix query

Create a matrix query to display the job, the salary for that job based on department number and the total salary for that job, for departments 20,50,80, and 90,giving each column and appropriate heading.

Questions by zecar   answers by zecar

Showing Answers 1 - 32 of 32 Answers

vssandilya

  • Sep 24th, 2006
 

dear friend,

try this code.

select department_id,job_id,sum(salary) from employees where

department_id in(20,50,80,9) group by rollup(department_id,job_id)

then reply me.

sandilya

  Was this answer useful?  Yes

Vijayalakshmi

  • Jul 6th, 2007
 

SELECT DISTINCT job,
      SUM(CASE deptno WHEN  20 THEN  sal  END)  "Dept 20",
       SUM(CASE deptno WHEN  50 THEN  sal  END)  "Dept 50",
      SUM(CASE deptno WHEN  80 THEN  sal  END)  "Dept 80",
      SUM(sal)  "Total"
FROM emp
GROUP BY job;

geethika

  • Oct 10th, 2007
 

select distinct job,
sum(decode(deptno,20,sal) ) "20"
,sum(decode(deptno,50,sal) )"50"
,sum(decode(deptno,80,sal) ) "80"
,sum(decode(deptno,90,sal) )"90"
,sum(sal) "total"
from emp
group by job;

  Was this answer useful?  Yes

select * from (select job,sum(decode(deptno,20,sal)) dept20,
                           sum(decode(deptno,50,sal)) dept50,
                           sum(decode(deptno,80,sal)) dept80,
                           sum(decode(deptno,90,sal)) dept90,
                           sum(sal) total_sal
        from emp group by job) order by 1

select job_id,salary,department_id,avg(salary) "Average Salary"
from employees
where department_id in(20,80,90,50)
group by job_id,salary,department_id
order by job_id

  Was this answer useful?  Yes

ARAVINDA

  • Aug 3rd, 2011
 

Code
  1. SELECT  *

  2.   FROM  (SELECT job,

  3.                 SUM(DECODE(deptno,10,sal)) DEPT10,

  4.                 SUM(DECODE(deptno,20,sal)) DEPT20,

  5.                 SUM(DECODE(deptno,30,sal)) DEPT30,

  6.                 SUM(DECODE(deptno,40,sal)) DEPT40

  7.            FROM scott.emp

  8.        GROUP BY job)


  9.  

  10. SELECT  *

  11.   FROM  (SELECT job,

  12.                 SUM(DECODE(deptno,10,sal)) DEPT10,

  13.                 SUM(DECODE(deptno,20,sal)) DEPT20,

  14.                 SUM(DECODE(deptno,30,sal)) DEPT30,

  15.                 SUM(DECODE(deptno,40,sal)) DEPT40

  16.            FROM scott.emp

  17.        GROUP BY job)


  18.  

  Was this answer useful?  Yes

Mahesh Bhiosale

  • Aug 19th, 2011
 

Hi bro, I had recentely learn about matrix queries. By using following code into oracle 9i or sql server 2005 you can create matrix query to get desired result as per ur wish.


Code
  1. SELECT  *

  2.   FROM  (SELECT job_id,

  3.                 SUM(DECODE(department_id,10,salary)) DEPT10,

  4.                 SUM(DECODE(department_id,20,salary)) DEPT20,

  5.                 SUM(DECODE(department_id,30,salary)) DEPT30,

  6.                 SUM(DECODE(department_id,40,salary)) DEPT40

  7.            FROM employees

  8.        GROUP BY job_id)

  Was this answer useful?  Yes

Neha

  • Sep 25th, 2011
 

select job in job, sal in salary, sum(salary) in total_sal
where job=(select job in job where deptno=20 and 50 and 80 and 90)
;

  Was this answer useful?  Yes

sachin

  • Apr 18th, 2012
 

select job,
sum(decode(deptno,10,sal)) deptno10,
sum(decode(deptno,20,sal)) deptno20,
sum(decode(deptno,30,sal)) deptno30,
sum(decode(deptno,40,sal)) deptno40
from emp group by job order by 1;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions