2017-01-06 14 views
1

我有這樣的桌子;將不同的列排序爲一個順序?

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的

+3

你在下面得到了一些工作答案,但真正的問題是你的表格結構,它會更好o將其標準化,以便您有一個student_name字段,並且您可以添加另一個字段用於標識對,如'pair_id'。 –

回答

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; 
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;