如何从表中获取每第10行,即10th,20th等。但是如果该表中的行少于10行,则应该获取最后一行。
这可以是此问题中此查询的高级形式 - How can I select 10th, 20th, 30th ... row of the result of another select query
我已经做了类似的事情来选择每隔10行 -
select case_id
from
(select case_id,rownum as seq
from table
where from = 'A' and to ='B'
order by ID
)
where mod(seq,10) = 0
但是如果内部查询的结果少于10行,则应该获取最后一行。
OBS:数据库 - Oracle 12c
答案 0 :(得分:2)
SELECT ColumnA,
ColumnB,
ColumnC
FROM (
SELECT ROWNUM rn,
COUNT(*) OVER ( ORDER BY NULL ) mx,
q.ColumnA,
q.ColumnB,
q.ColumnC
FROM (
SELECT ColumnA,
ColumnB,
ColumnC
FROM your_table
ORDER BY ColumnA
) q
)
WHERE MOD( rn, 10 ) = 0
OR ( mx = rn AND mx < 10 );