Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
SQL
Declare @paramList VARCHAR(MAX)
SET @paramList = STUFF((
                    SELECT DISTINCT ',[' + Question + ']'
                     FROM View_S FOR XML PATH('')
                    )  ,1,1,'')
Print @paramList


DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT RollNo,SName,'; + @paramList
+ 'FROM( SELECT RollNo,SName,Results,Question FROM View_S )src
PIVOT(Max(Results) FOR Question IN (' + @paramList +')) pvt'

EXEC sp_executesql @query


i write this query and its working this output is
___________
Q4|Q3|Q1|Q2
-----------

but i want it in order wise
___________
Q1|Q2|Q3|Q4
-----------
what i do please help me

[Edit]Code block added[/Edit]
Posted
Updated 9-Mar-13 1:48am
v2

You just need to add ORDER BY RollNo to your SQL query, followed by the order (acceding or descending). For acceding use ASC and for decending use DESC

Further reading:
http://www.w3schools.com/sql/sql_orderby.asp[^]
 
Share this answer
 
Try this:
SQL
Declare @paramList VARCHAR(MAX)
SET @paramList = STUFF((
                    SELECT DISTINCT '],[' + Question
                    FROM View_S
                    ORDER BY  '],[' + Question
                    FOR XML PATH('')) ,1,2,'') + ']'
PRINT @paramList
 
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