我試圖讓每個組組合中的第n行,例如前n行
用下面的輸入我想獲得每個產品區相結合的前兩排,有序按日期:
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
你的查詢有什麼問題?你的問題是什麼? –