各グループの組み合わせに対して最初のn行を取得しようとしています。たとえば、グループごとの最初のn行
次の入力では、製品領域の組み合わせごとに最初の2行を取得したいと思います日によって:
product area date units
A US 2015/08/07 10
A US 2015/08/08 12
A US 2015/08/09 15
A UK 2015/08/07 13
A UK 2015/08/10 11
B US 2015/08/07 16
B US 2015/08/08 17
B US 2015/08/09 12
B UK 2015/08/07 10
B UK 2015/08/08 09
B UK 2015/08/09 07
は、私が試した
product area date units
A US 2015/08/07 10
A US 2015/08/08 12
A UK 2015/08/07 13
A UK 2015/08/10 11
B US 2015/08/07 16
B US 2015/08/08 17
B UK 2015/08/07 10
B UK 2015/08/08 09
返します:
with ordered as (select product, area, date, units,
row_number() over (partition by product, area order by date asc) as date_rank
from mytable)
select product, area, date, units
from ordered
where date_rank <= 2
質問にはどのような問題がありますか?あなたの質問は何ですか? –