我试图让每个组组合中的第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
你的查询有什么问题?你的问题是什么? –