ID Num
1 10
2 52
7 6
4 79
我现在想求出Num值最大的那一行的ID
上例中Num最大79,对应ID是4
oracle有没有简便的写法
9 个解决方案
#1
你想要多简便呢
把你的代码写出来
看看能不能给你更简便的写法
把你的代码写出来
看看能不能给你更简便的写法
#2
我现在这样写的
select ID from (
select a.*,rank() over(partition by ID,order by num desc) rk from a)
where rk = 1
#3
select ID from a where NUM =(select max(NUM) from a);
#4
这样如果两个num一样大,他返回的ID和NUM的值对应不起来
假如我还有2列
HIST GROUP ID Num
2012/01/05 A 1 10
2011/05/06 B 2 52
2012/08/09 C 7 6
2011/07/05 D 4 79
。。。
我要返回某个Group,num最大值,对应的ID
#5
with a as(
select 1 id,10 num from dual union all
select 2,52 from dual union all
select 7,6 from dual union all
select 4,79 from dual)
select ID from (
select a.*,rank() over(order by num desc) rk from a)
where rk = 1
select 1 id,10 num from dual union all
select 2,52 from dual union all
select 7,6 from dual union all
select 4,79 from dual)
select ID from (
select a.*,rank() over(order by num desc) rk from a)
where rk = 1
#6
with a as(
select '2012/01/05' history,'A' group1,1 id,10 num from dual union all
select '2012/05/06','B',2,52 from dual union all
select '2012/08/09','C',7,6 from dual union all
select '2012/07/05','D',4,79 from dual union all
select '2012/01/05','E',5,79 from dual)
select id,num from (
select a.*,rank() over(partition by group1 order by num desc) rk from a)
where rk = 1
每个group中num最大值
select '2012/01/05' history,'A' group1,1 id,10 num from dual union all
select '2012/05/06','B',2,52 from dual union all
select '2012/08/09','C',7,6 from dual union all
select '2012/07/05','D',4,79 from dual union all
select '2012/01/05','E',5,79 from dual)
select id,num from (
select a.*,rank() over(partition by group1 order by num desc) rk from a)
where rk = 1
每个group中num最大值
#7
select b.id from a b where not exists(select 1 from a where num>b.num)
#8
select id from a where point = (select MAX(num) from a)
#9
select a.gr,t.id from (select t.group gr,max(t.num) nu
from t
group by t.group) a,t
where t.group=a.gr and t.num= a.nu
选择每个group下num最大的id
#1
你想要多简便呢
把你的代码写出来
看看能不能给你更简便的写法
把你的代码写出来
看看能不能给你更简便的写法
#2
我现在这样写的
select ID from (
select a.*,rank() over(partition by ID,order by num desc) rk from a)
where rk = 1
#3
select ID from a where NUM =(select max(NUM) from a);
#4
这样如果两个num一样大,他返回的ID和NUM的值对应不起来
假如我还有2列
HIST GROUP ID Num
2012/01/05 A 1 10
2011/05/06 B 2 52
2012/08/09 C 7 6
2011/07/05 D 4 79
。。。
我要返回某个Group,num最大值,对应的ID
#5
with a as(
select 1 id,10 num from dual union all
select 2,52 from dual union all
select 7,6 from dual union all
select 4,79 from dual)
select ID from (
select a.*,rank() over(order by num desc) rk from a)
where rk = 1
select 1 id,10 num from dual union all
select 2,52 from dual union all
select 7,6 from dual union all
select 4,79 from dual)
select ID from (
select a.*,rank() over(order by num desc) rk from a)
where rk = 1
#6
with a as(
select '2012/01/05' history,'A' group1,1 id,10 num from dual union all
select '2012/05/06','B',2,52 from dual union all
select '2012/08/09','C',7,6 from dual union all
select '2012/07/05','D',4,79 from dual union all
select '2012/01/05','E',5,79 from dual)
select id,num from (
select a.*,rank() over(partition by group1 order by num desc) rk from a)
where rk = 1
每个group中num最大值
select '2012/01/05' history,'A' group1,1 id,10 num from dual union all
select '2012/05/06','B',2,52 from dual union all
select '2012/08/09','C',7,6 from dual union all
select '2012/07/05','D',4,79 from dual union all
select '2012/01/05','E',5,79 from dual)
select id,num from (
select a.*,rank() over(partition by group1 order by num desc) rk from a)
where rk = 1
每个group中num最大值
#7
select b.id from a b where not exists(select 1 from a where num>b.num)
#8
select id from a where point = (select MAX(num) from a)
#9
select a.gr,t.id from (select t.group gr,max(t.num) nu
from t
group by t.group) a,t
where t.group=a.gr and t.num= a.nu
选择每个group下num最大的id