通过一段比较复杂的sql语句 (……)取得如下结果:id v1 v2 v3 v4
001 a xx 1
001 b 2
001 c 3
002 d 1
002 e 2
003 f 1
003 g 2
003 f 3
等等数据量比较大
我想要的结果是
id分别为001,002,003 对应的v4值最大的一行
即:
001 c xx 3
002 e 2
003 f 3
001 a xx 1
001 b 2
001 c 3
002 d 1
002 e 2
003 f 1
003 g 2
003 f 3
等等数据量比较大
我想要的结果是
id分别为001,002,003 对应的v4值最大的一行
即:
001 c xx 3
002 e 2
003 f 3
001 a xx 1
001 b 2
001 c 3
002 d 1
002 e 2
003 f 1
003 g 2
003 f 3
……
数据比较多,,
(Select c.id,max(c.v4) From Table c group by c.id) B
where a.id = b.id
and a.v4 = b.v4
select id,v1,v2,v3,v4 from a where id in (select id,max(v4) from a group by id);
id v1 v2 v3 v4
001 a xx 1
001 b 2
001 c 3
002 d 1
002 e 2
003 f 1
003 g 2
003 f 3
……
这些数据不是一个表,,我是也是用(……)其中省略一段sql语句从N个表中取出来的,,难不成让我Select a.id,a.v1,a.v2,a.v3,max(b.v4) From (……) a ,
(Select c.id,max(c.v4) From (……) c group by c.id) B
where a.id = b.id
and a.v4 = b.v4这样效率是不是差了点。
from
(select row_number()over(partition by id order by v4 desc) rn,id,v1,v2,v3,v4
from tablename)
where rn=1
;
first_value(v2) over(partition by id order by v4 desc) v2,
first_value(v3) over(partition by id order by v4 desc) v3,
max(v4) over (partition by id) v4
from tablename;