Script
DECLARE @Names AS TABLE(ID INT, Name VARCHAR(MAX))
INSERT INTO @Names
VALUES (1, 'Paul'), (2, 'John'), (3, 'Tayler');
DECLARE @PersonInfo AS TABLE
(
ID INT,
NameID INT,
Subject VARCHAR(100),
Marks FLOAT
)
INSERT INTO @PersonInfo
VALUES (1, 1, 'Maths', 95.34), (2, 2, 'Science', 32.12),
(3, 3, 'History', 23.21), (4, 2, 'Maths', 32.4),
(5, 3, 'Science', 60.34), (6, 1, 'Music', 60.23);
SELECT * FROM @PersonInfo
I have tried to generate the dynamic columns using PIVOT expression, but no luck. Can anyone give me an idea or the solution to generate the below output.
Thanks in advance
Expected solution:
Subject Paul John Tayler
-----------------------------------
Maths 95.34 32.4
Science 32.12 60.34
History 23.21
Music 60.23