我正在尝试从查询中获取包含前 N 行的结果集,并将剩余的行汇总为一行。我提出了如下查询 - 我需要有关使用任何内置 oracle sql 函数的建议,这些函数可以帮助解决这种情况并消除我在这个 sql 中的很多冗余。
select label, count_id from
(
select table1.NAME as label, count(table1.id) as count_id,
ROW_NUMBER() OVER (order by count(table1.id) desc) AS rn
from table1
where table1.NAME like 'D%'
group by table1.NAME
)
where rn <= 9 -- get top 9 rows
union
select 'Other' as label, sum(count_id) as count_id from
(
select label, count_id from
(
select table1.NAME as label, count(table1.id) as count_id,
ROW_NUMBER() OVER (order by count(table1.id) desc) AS rn
from table1
where table1.NAME like 'D%'
group by table1.NAME
)
where rn > 9 -- get rows after row-num 9
)
如果您对改进此查询有任何建议,请分享。