Reputation: 1731
I have a table tblInput with two col1 and col2.
I need to get the following output through a query.
So far i ve done this:
select col1 + col2 from tblInput order by col1,col2
but this giving the ouput in single column. I want it in a matrix.
How to do it?
Upvotes: 0
Views: 1005
Reputation: 5792
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
, sum(sal) TOTAL
FROM scott.emp
GROUP BY job)
ORDER BY 1
/
JOB DEPT10 DEPT20 DEPT30 DEPT40 TOTAL
--------- ---------- ---------- ---------- ---------- ----------
ANALYST 6000 6000
CLERK 1300 1900 950 4150
MANAGER 2450 2975 2850 8275
PRESIDENT 5000 5000
SALESMAN 5600 5600
Upvotes: 1
Reputation: 6289
You need to use PIVOT
in your query. Please refer to example at :
http://www.mssqltips.com/sqlservertip/1019/crosstab-queries-using-pivot-in-sql-server/
Upvotes: 2