存储过程(Pivot Columns Rankin) [英] Stored Proc (Pivot Columns Rankin )
本文介绍了存储过程(Pivot Columns Rankin)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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写这个查询及其工作这个输出是
___________
Q4 | Q3 | Q1 | Q2
-----------
但是我想按顺序明白
___________
Q1 | Q2 | Q3 | Q4
-----------
我的帮助请帮助我
已添加代码块[/编辑]
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
Code block added[/Edit]
推荐答案
您只需要将 ORDER BY RollNo添加到您的SQL查询,然后是订单(加入或降序)。对于加入使用 ASC 和下降使用 DESC
进一步阅读:
http://www.w3schools.com/sql/sql_orderby.asp [ ^ ]
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[^]
试试这个:
Try this:
Declare @paramList VARCHAR(MAX)
SET @paramList = STUFF((
SELECT DISTINCT '],[' + Question
FROM View_S
ORDER BY '],[' + Question
FOR XML PATH('')) ,1,2,'') + ']'
PRINT @paramList
这篇关于存储过程(Pivot Columns Rankin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文