分组数据——取最新一条数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/heqiang525/article/details/88233938

确实挺有用的,哈哈!!!,试了很多次。

select * from (select row_number() over (partition by cardid order by createdate desc)b, a.* from sev_account a) where b=1

1、上式中的*可以根据自己的需要改成所需要查询的字段;

2、partition by cardid, 是对cardid进行分组,也可以根据自己需要改变;

3、order by createdate desc,是取createdate(时间字段)最新的一条数据,也可以取最久的一条(通过asc);

以上取得是cardidz最新的一条数据。

也可以将查询的数据导入一个新表:

create table 表名 as select * from (select row_number() over (partition by carid order by createdate desc)b, a.* from sev_account a) where b=1

猜你喜欢

转载自blog.csdn.net/heqiang525/article/details/88233938
今日推荐