oracle group by某个字段,但是要显示其他字段

使用first_value函数

select distinct col1,
first_value(col2) over (partition by col1 order by col2 asc)
from tmp

使用over开窗函数

select col1, col2 from (
  select col1, col2, 
  row_number() over (partition by col1 order by col2 asc) as rownumber 
  from tmp
) foo
where rownumber = 1

如果有更多列,加个distinct即可

select col1,col2, col3 from tmp where col2 in
(
select distinct 
first_value(col2) over (partition by col1 order by col2 DESC) as col2
from  tmp
--WHERE you decide conditions
)

你可能感兴趣的:(oracle)