Oracle:查询各组最新的一条记录
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";
写法二: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";
查询结果为: