将不同的列整理成一个订单?

时间:2017-01-06 20:05:22

标签: sql sql-server sql-server-2008

我有一张这样的桌子;

Student_Name1   mark1  Student_Name2    mark2
-------------- ------ ---------------  --------
Kevin           77       Peter          78
Andrew          91       David          17
Scott           46       Bradley        28

如何将上表中的mark1mark2按顺序排列,将所有名称和所有点组合在一起,如下所示?

Student_Name   mark
-------------- ------
Andrew          91
Peter           78
Kevin           77
Scott           46
Bradley         28
David           17

我正在使用MSSQL Server 2008 R2

2 个答案:

答案 0 :(得分:4)

使用UNION ALL

Select    Student_Name1 As Student_Name,
          Mark1         As Mark
From      YourTable
Union All
Select    Student_Name2 As Student_Name,
          Mark2         As Mark
From      YourTable
Order By Mark Desc;

答案 1 :(得分:3)

这是一个奇怪的桌面设计,但您可以将UNION用于此目的,例如

select * from (
select Student_Name1 as Student_Name,  mark1 as mark from student
union all
select Student_Name2 ,  mark2 from student ) xxx
order by mark desc;