首页 > 代码库 > 关于sql语句 先分组再排序然后取每组的第一条数据

关于sql语句 先分组再排序然后取每组的第一条数据

select  ranked.等待时间 from
(
select *, ROW_NUMBER() over(partition by tb.PATIENT_ID order by tb.等待时间 ASC) as rowNum
from (select a.PATIENT_ID as PATIENT_ID, DATEDIFF(HOUR,b.ADMISSION_DATE, a.OPER_START_DATE) as 等待时间
from PAT_OPERATION as a,PAT_IN_VISIT as b
where OPERATION_CODE=‘SETMI_OP01‘ and a.PATIENT_ID=b.PATIENT_ID and DISCHARGE_DATE>=‘2014-04-01‘
and DISCHARGE_DATE<‘2014-07-01‘) tb
) ranked
where ranked.rowNum <=1

因为每个id对应两个等待时间 我取第一个

关于sql语句 先分组再排序然后取每组的第一条数据