Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(subjects) 
                    from mark_entry
                    group by subjects
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT * from 
             (
               select student_name,roll_no,standard,section,subjects,cast(mark as int)as mark from mark_entry
            ) x
            pivot 
            (
                sum(mark)
                for subjects in (' + @cols + ')
            ) p '

execute(@query)


This query i have used to display the record of the all the student in school using pivot table
Now I have to display only one student record i do no know where to the WHERE condition
Please Help me
Posted
Updated 25-Sep-13 21:37pm
v2

SQL
set @query = 'SELECT * from
(select student_name,roll_no,standard,section,subjects,cast(mark as int)as mark from mark_entry) x
pivot
(sum(mark) for subjects in (' + @cols + '))p Where student_name =''StudentName'''
 
Share this answer
 
v4
Comments
Mohamed ansari 26-Sep-13 3:38am    
the following error
Msg 207, Level 16, State 1, Line 12
Invalid column name 'StudentName'.
Raja Sekhar S 26-Sep-13 3:41am    
sorry u have to use Single quotes.. Check the Updated Solution
Raja Sekhar S 26-Sep-13 4:05am    
Did it Solve your Problem..?
Mohamed ansari 26-Sep-13 4:44am    
Thanks Bro
SQL
set @query = 'SELECT * from
(select student_name,roll_no,standard,section,subjects,cast(mark as int)as mark from mark_entry
WHERE StudentName = ''StudentName'') x
pivot
(sum(mark) for subjects in (' + @cols + '))p
 Where student_name =''StudentName'''
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900