嗯,是的 - 如果您是新手,这样的问题可能会“棘手”。您可能知道应该使用某种 order by
子句,但是 - 如何使用?哪里?
row_number
分析函数对数据进行“排序”:
col1
col2
join
,以便您匹配适当的值。
样本数据:SQL> select * from test;
COL1 COL2
---------- ----------
1 4
3 5
7 9
2 6
3 1
查询:
SQL> with
2 t1 as
3 (select col1, row_number() over (order by col1) rn from test),
4 t2 as
5 (select col2, row_number() over (order by col2 desc) rn from test)
6 select a.col1, b.col2
7 from t1 a join t2 b on a.rn = b.rn
8 order by a.col1;
COL1 COL2
---------- ----------
1 9
2 6
3 5
3 4
7 1
SQL>
(分析函数、CTE、子查询、order by、join),您可以进一步探索并尝试编写自己的解决方案。