Hi, I am trying to replicate an access crosstab query
I have use PIVOT but this is not working as this requires an aggregate function.
However , I would like output in this format:
Class | History | Maths | Art |
Class1 | Student1 | Student2 | Student1 |
Class1 | Student5 | Student6 | Student4 |
Class1 | Student6 | Student6 |
Any thoughts ?
DECLARE @School TABLE (Class VARCHAR(10), [Subject] VARCHAR(20), Student VARCHAR(20))
INSERT INTO @School(Class, [Subject], Student) VALUES('Class1','History','Student5')
INSERT INTO @School(Class, [Subject], Student) VALUES('Class1','History','Student6')
INSERT INTO @School(Class, [Subject], Student) VALUES('Class1','Maths','Student2')
INSERT INTO @School(Class, [Subject], Student) VALUES('Class1','Maths','Student6')
INSERT INTO @School(Class, [Subject], Student) VALUES('Class1','Art','Student1')
INSERT INTO @School(Class, [Subject], Student) VALUES('Class1','Art','Student4')
INSERT INTO @School(Class, [Subject], Student) VALUES('Class1','Art','Student6')
-- Pivot table
SELECT Class,
[History], [Maths], [Art]
FROM
(SELECT Class, [Subject], Student
FROM @School) AS tbl
PIVOT(Min(Student) FOR [Subject] IN ([History], [Maths], [Art])) As pvt