Oracle:查询各组最新的一条记录

时间:2023-01-07 15:03:34

oracle中怎么查询各组中最新的一条记录呢?比如说现在有一个表中几条数据如下:


有两种写法:
写法一:over partition by 分析函数

SELECT * FROM (select ID_,COMPANY_NAME,USAGE_RATE,DETECTION_RATE,ACCEPTABILITY_RATE,CREATE_TIME,MAX(CREATE_TIME) over(partition by COMPANY_NAME)
as "atime" from SPEC_RATE_ORIGIN) x where CREATE_TIME = "atime";
1
2
写法二:inner join

SELECT A.* FROM SPEC_RATE_ORIGIN A INNER JOIN (SELECT COMPANY_NAME,MAX(CREATE_TIME) AS "atime"
FROM SPEC_RATE_ORIGIN group by COMPANY_NAME) B ON A.COMPANY_NAME = B.COMPANY_NAME AND A.CREATE_TIME = B."atime";
1
2
查询结果为:


---------------------
作者:Ryze丶
来源:CSDN
原文:https://blog.csdn.net/ShelleyLittlehero/article/details/85041768
版权声明:本文为博主原创文章,转载请附上博文链接!