mysql:
oracle与mysql语法有点不同,如果还是这样写会报错:group by 不是表达式
用row_number over 轻松解决
转载自 https://blog.csdn.net/nux_123/article/details/45037719
解决过程:
1.查看表中的重复记录
select
t.id,
t.device_id,
t.update_dtm,
t.test_result
from DEVICE_INFO_TBL t
2.标记重复的记录
select
t.id,
t.device_id,
t.update_dtm,
t.test_result,
row_number() OVER(PARTITION BY device_id ORDER BY t.update_dtm desc) as row_flg
from DEVICE_INFO_TBL t
3.过滤重复数据,取得最新的记录
select
temp.id,
temp.device_id,
temp.update_dtm,
temp.test_result
from (
select
t.id,
t.device_id,
t.update_dtm,
t.test_result,
row_number() OVER(PARTITION BY device_id ORDER BY t.update_dtm desc) as row_flg
from DEVICE_INFO_TBL t ) temp
where temp.row_flg = '1'