任何人都可以纠正我如何错误地转动行和列吗?
--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
答案 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