あるグループごとに上位〇件ずつデータを取得して比較したいという際にwindow関数を使う。
テーブル
sales_t
category | product | sales |
---|---|---|
食品 | りんご | 30 |
食品 | みかん | 20 |
食品 | バナナ | 10 |
筆記用具 | ペン | 40 |
筆記用具 | 消しゴム | 10 |
筆記用具 | 赤ペン | 30 |
categoryごとに上位2件ずつ取得する
select * from (
select category
, product
, row_number() over (partition by category order by sales desc) as row
from sales_t
)
where row <= 2;
件数の多いものから取得する
select * from (
select category
, product
, count(*) as cnt
, row_number() over (partition by category order by count(*) desc) as row
from sales_t
)
where row <= 2;