当前位置: 代码迷 >> Oracle开发 >> Oracle 如何求最大值的行
  详细解决方案

Oracle 如何求最大值的行

热度:131   发布时间:2016-04-24 06:42:26.0
Oracle 怎么求最大值的行
Oracle 中有如下行

ID  value
1     1
1     2
2     1
2     2
2     3

用什么样的 SQL 能得到
ID  value
1     2
2     3
------解决方案--------------------
with t as
 (select 1 id, 1 value
    from dual
  union all
  select 1 id, 2 value
    from dual
  union all
  select 2 id, 1 value
    from dual
  union all
  select 2 id, 3 value
    from dual
  union all
  select 2 id, 2 value from dual)
select t1.id, t1.value
  from (select t.*,
               row_number() over(partition by id order by value desc) rn
          from t) t1
 where t1.rn = 1;

------解决方案--------------------
SELECT id,max(VALUE)
  FROM 表名
 GROUP BY id;
  相关解决方案