按值获取第一行组

时间:2014-05-20 10:44:54

标签: sql oracle group-by

我错过了一块拼图。我想按strid选择c_date订单的第一行,然后按strid分组。

我的sql是:

--there should be some 'FIRST' function on strid
select strid over(partition by strid order by c_date) 
from tab
where c_date > :1
  and c_date <= :2

标签

strid    VARCHAR2(255 CHAR)
c_date   TIMESTAMP(6) WITH TIME ZONE

我可以使用row_number,然后选择row_number=1,但我正在尝试避免此子查询,我正在搜索更优化的内容..

1 个答案:

答案 0 :(得分:1)

尝试first_value

-- do you really want to partition by strid?
select first_value(strid) over (partition by strid order by c_date) 
  from tab
 where (c_date > :1)
   and (c_date <= :2)