简体   繁体   English

使用sql存储过程将列值移动到下一行

[英]To move column value to next row using sql stored procedure

In the below query i am selecting the values from the table it has 4 columns name,age,class,rollno.And i want to display it in 2 columns (ie)to make 2 column value to move to next row and 4 column value to next row.Pls help me to do this. 在下面的查询中,我从表中选择值,它具有4列名称,年龄,类别,rollno。并且我想在2列中显示它(例如)以使2列值移动到下一行和4列值到下一行。请帮助我做到这一点。

StudentID|name/age|class/Roll no|
1          xxx       2
1           5        123556
2          yyy        2
2           5        123557

Select studentid,name.age,class,roll no from student

You can UNION ALL and use 2 queries. 您可以UNION ALL并使用2个查询。 I had to cast the age as a varchar so that it could go in the name.age column 我必须将年龄转换为varchar,以便可以在name.age列中输入

SQL Fiddle Example SQL小提琴示例

select id, name as 'name.age', class as 'class.rollno'
from student
union all
select id, cast(age as varchar), rollno
from student
order by id, name desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM