场景:我有一个下表,我想以简单的方式对两个不同的列进行排序。
TestTable的
List Val
--------
a 20
c 10
b 30
结果:
List Val
--------
a 10
b 20
c 30
我尝试过以下查询,但无法对两列进行排序
Select Col, val
From Dummy
Order By Col, val asc
但是我在下面的查询
上得到了理想的结果With Q As
(
Select
Col, Val,
Row_Number() Over (Order By Col) TT1
From TestTable
),
Q1 As
(
Select
Col, Val,
Row_Number() Over (Order By Val) TT2
From TestTable
)
Select
Q.Col, Q1.Val
From
Q, Q1
Where
TT1 = TT2
但我需要知道有没有其他简单的方法来实现它。我们能否通过JOIN获得结果。
答案 0 :(得分:0)
要获得所需的输出,您可以执行以下操作:
SELECT ListSort.list, ValSort.val
FROM
(
SELECT TOP 100 PERCENT ROW_NUMBER() OVER(ORDER BY list) AS ID, list
FROM TestTable
ORDER BY list
) ListSort INNER JOIN
(
SELECT TOP 100 PERCENT ROW_NUMBER() OVER(ORDER BY val) AS ID, val
FROM TestTable
ORDER BY val
) ValSort ON ListSort.ID = ValSort.ID