oracle--分组后获取每组数据第一条数据

时间:2022-10-10 00:27:21
SELECT *        
   FROM (SELECT ROW_NUMBER() OVER(PARTITION BY cc.queuename ORDER BY cc.enroldate DESC) rn,        
         cc.*        
         FROM 
         (select * from 
(select a.patname,a.queuesign,a.queuename,a.status,a.checkroom,a.areapart,a.enroldate
from qs_register a where a.status='就诊中'
group by a.patname,a.queuesign,a.queuename,a.status,a.checkroom,a.areapart,a.enroldate
order by a.queuename,a.status,a.enroldate) bb)cc
         )        
  WHERE rn = 1  ;

  参考:

[sql]
SELECT *        
   FROM (SELECT ROW_NUMBER() OVER(PARTITION BY x ORDER BY y DESC) rn,        
         test1.*        
         FROM test1)        
  WHERE rn = 1  ;

  注:同理而言,需要查第几条数据或需要查询指定条数的数据——where rn=N or where rn in(num1,num2,numx,......)