SQL SERVER 2008 – How does one code a matrix report in SQL

Look at this example query with sample output:

SELECT  *
FROM  (SELECT job,
sum(decode(deptno,10,sal)) DEPT10,
sum(decode(deptno,20,sal)) DEPT20,
sum(decode(deptno,30,sal)) DEPT30,
sum(decode(deptno,40,sal)) DEPT40
FROM scott.emp
GROUP BY job)
ORDER BY 1;

JOB              DEPT10     DEPT20     DEPT30     DEPT40
———       ———-    ———-     ———-     ———-
ANALYST                       6000
CLERK           1300       1900          950
MANAGER  2450       2975          2850
PRESIDENT 5000
SALESMAN                                        5600

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s