PIVOT Table: Row to Column using temporary table

declare @table table
(
Roll int,
CT int,
Marks decimal(8,2)
)
declare @string nvarchar(max)
declare @tblname nvarchar(100)
set @tblname=’CT_Marks’
set @string=’create table #’ + @tblname + ‘ (‘
insert into @table
(
Roll,
CT,
Marks
)
SELECT dbo.T_Term_Class_Test.StudentId, dbo.T_Term_Class_Test_Defination.ClassTestNumber, dbo.T_Term_Class_Test.ConvertedMarks
FROM dbo.T_Term_Class_Test INNER JOIN dbo.T_Term_Class_Test_Defination ON dbo.T_Term_Class_Test.CTId = dbo.T_Term_Class_Test_Defination.CTId

SELECT *
FROM @table
PIVOT
(
SUM(Marks)
FOR [CT] IN ([1],[2],[3],[4])
)
AS p

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s