在列上的数据透视表deptno和行上的计数和工资?

时间:2013-04-24 09:58:18

标签: sql sql-server sql-server-2008 pivot pivot-table

任何人都可以纠正我如何错误地转动行和列吗?

--Trying to count no. of employees in each dept and pivoting it as deptno on columns and counts of no. of employees
-- rows

SELECT 10, 20, 30
FROM emp
PIVOT
(
count(deptno)
FOR empno IN ([10],[20],[30])
)
as pt

--Trying to sum of salary in each dept and pivoting it as deptno on columns and sum of salary of rows but same repeating nature
-- rows

SELECT 10, 20, 30
FROM emp
PIVOT
(
deptno(deptno)
FOR deptno IN ([10],[20],[30])
)
as pt

1 个答案:

答案 0 :(得分:1)

试试这个 -

DECLARE @temp TABLE (deptno INT)

INSERT INTO @temp (deptno)
VALUES (10),(20),(20), (40),(30)

SELECT [10], [20], [30]
FROM @temp
PIVOT
(
    COUNT(deptno)
    FOR deptno IN ([10], [20], [30])
) pt