麻烦问下,我现在Oracle通过左连接得到一个表,里面包括ID,NAME,TIME,NUM共计4个字段,现在ID和NAME是对应关系,有重复行,但是TIME和NUM是每行都不同的,我现在需要提取出相同的ID和NMAE中NUM最大值的那一行,其余的不要,请问该怎么写呢?
最终效果如:
ID NAME TIME NUM
1 A 02.30 569
2 B 15.30 956
(select MAX(num) from tb group by id)---num值最大的行
delete from tb where num not in (select MAX(num) from tb group by id)---保留最大行,其余的删除
select from a.* , b.time from(select id, name, max(num) as num from t_test GROUP BY id,name)a, t_test b
where a.id = b.id and a.name = b.name and a.num = b.num
SELECT ID,NAME,MAX(NUM) from table GROUP BY ID,NAME HAVING count(*)>1
SELECT
*,
max( num )
FROM
表名
GROUP BY
id,
NAME
二楼的回答应该能出现正确答案,但是不够优雅
select t.*,row_number() over(partition by id,name order by num desc ) row_number from Test t where row_number = 1
select dinstinct id,name from 表民 order by num desc limit 1;
这样先去重,在大小排序,最后取最大的那个数据即可
复制上面
row_number()添加一行伪列 根据id,name分区 num 倒序
select t.*,row_number() over(partition by id,name order by num desc ) row_number from Test t where row_number = 1
select from a.* , b.time from(select id, name, max(num) as num from t_test GROUP BY id,name)a, t_test b
where a.id = b.id and a.name = b.name and a.num = b.num
应该都能满足需求