クエリから上位N行の結果セットを取得し、残りの行を1つの行にロールアップしようとしています。私は以下のようなクエリを思いつきました-私はこのシナリオを助け、このSQLにある多くの冗長性を排除することができる組み込みのOracleSQL関数を使用することについての提案を必要としています。
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
)
このクエリを改善するための提案があれば共有してください。