SQL> with test (a, b, c) as
2 (select 1, 2, 3 from dual union all
3 select 2, 3, 4 from dual
4 ),
5 temp as
6 (select a, b, c,
7 row_number() over (order by column_value, a) rn
8 from test cross join table(cast(multiset(select level from dual
9 connect by level <= 9
10 ) as sys.odcinumberlist))
11 )
12 select a, b, c
13 from temp
14 where rn <= 9
15 order by rn ;
A B C
---------- ---------- ----------
1 2 3
2 3 4
1 2 3
2 3 4
1 2 3
2 3 4
1 2 3
2 3 4
1 2 3
9 rows selected.
SQL>
with cte(a, b, c, rn, cnt, lvl) as (
select a, b, c, row_number() over(order by a, b, c) rn, count(*) over() cnt, 0 lvl from mytable
union all
select a, b, c, rn, cnt, lvl + 1 from cte where lvl * cnt + rn < 8
)
select a, b, c from cte order by lvl, rn
2条答案
按热度按时间7kqas0il1#
这样地?
它有什么作用?
第1-4行代表您的示例数据
cte公司
temp
(第5-11行)创建了所有这些行;row_number
是用来给他们排序的column_value
(把它当作level
伪柱,如果它离你更近)和a
列值(为什么?您的示例输出表明如此)最终查询(第12-15行)选择
rn <= 9
(因为你想得到9行)omhiaaxx2#
一种方法是递归查询。如果您想要9条记录,那么:
其思想是用
row_number()
数一数。然后我们生成新行,并在达到目标行数时立即停止。这比生成比需要更多的行,然后进行过滤更有效。db小提琴演示: