2013-03-07 42 views
0

如何获得以下输出?如何获得以下输出?

输入:

t1 
----------------- 
col1 col2 
---------------- 
2  a 
1  c 
3  b 
---------------- 

输出:

t1 
----------------- 
col1  col2 
---------------- 
1   a 
2   b 
3   c 
---------------- 
+1

哪里'B'输出从何而来? – 2013-03-07 13:09:17

+0

对不起,我改变了 – Madhav 2013-03-07 13:10:00

+0

这可能会帮助你:[排序两列不同顺序](http://www.java2s.com/Tutorial/Oracle/0040__Query-Select/Sorttwocolumnswithdifferentordering.htm) – 2013-03-07 13:13:06

回答

1
select C1.col1, C2.col2 
from 
    (select col1, row_number() over (order by col1) rn 
    from t1) C1 
join 
    (select col2, row_number() over (order by col2) rn 
    from t1) C2 
on C1.rn=C2.rn 
order by C1.rn 
+0

'a.rn'应该是't1.rn'(用'your_table',你指的是OP的't1',对吗?可能比't1/t2'更适合使用其他表别名)。 – 2013-03-07 13:36:43

+1

谢谢,我已经纠正。 – 2013-03-07 13:39:25

+0

一个额外的小修正:'从t2'应该是'从t1'(OP的表) - 然后,它就像一个魅力:) – 2013-03-07 13:40:16

2

您可以尝试使用行号,如:

SELECT row_number() OVER (ORDER BY a.col2) as col1, col2 
FROM t1 a ORDER BY a.col2 
+0

我已经不同地理解了这个问题,因为他要求分开重新排序。你只需重新分配ID。 :)不知道OP在询问什么。 – 2013-03-07 13:43:40

+0

尽管我同意弗洛林说这个问题没有明确说明,但我怀疑Harry的解释很可能是正确的 - 他们只是想在表格中“重新排序”id。无论如何,另一种解释没有任何意义。 – 2013-03-12 07:33:59

0

试试这个..

select col1,col2 from 
(select col1,rownum rn from(select col1 from t1 order by col1)) a, 
(select col2,rownum rn from(select col2 from t1 order by col2)) b 
where a.rn=b.rn