i am getting like
Subjects |
1 |
2 |
3 |
DRAWING |
17 |
19 |
59 |
ENGLISH |
19 |
15 |
56 |
EVS |
18 |
18 |
57 |
GEN. KNOWLEDGE |
18 |
20 |
55 |
HINDI |
18 |
16 |
54 |
MATHEMATICS |
19 |
19 |
58 |
MORAL EDUCATION |
19 |
20 |
59 |
and i want
Subjects |
1 |
2 |
3 |
G total |
DRAWING |
17 |
19 |
59 |
95 |
ENGLISH |
19 |
15 |
56 |
90 |
EVS |
18 |
18 |
57 |
93 |
GEN. KNOWLEDGE |
18 |
20 |
55 |
93 |
HINDI |
18 |
16 |
54 |
88 |
MATHEMATICS |
19 |
19 |
58 |
96 |
MORAL EDUCATION |
19 |
20 |
59 |
98 |
Total |
128 |
127 |
398 |
653 |
here is my stored procedure
CREATE proc [dbo].[sp_showmarks](@st_id int ,@result int output)
as
set @result=-1
IF( EXISTS( SELECT * FROM Marks WHERE Student_Id = @st_id))
BEGIN
SELECT *
INTO #TempMarks
FROM
(SELECT Student_Id,Subject,Term,Obt_Marks
FROM Marks WHERE Student_Id = @st_id ) as x
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX),@ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName = ISNULL(@ColumnName + ',','')+ QUOTENAME(Term) FROM (SELECT DISTINCT Term
FROM #TempMarks) AS [Terms]
SET @DynamicPivotQuery = ';WITH CTE AS(SELECT Student_Id,Subject,Term,Obt_Marks FROM #TempMarks )
SELECT Subject,'+@ColumnName+' FROM CTE
PIVOT (MAX(Obt_Marks) FOR [Term] IN('+@ColumnName+')) p
ORDER BY Student_Id DESC'
EXEC(@DynamicPivotQuery)
drop table #TempMarks
where i have to add code for sum of rows and columns