表中字段:phonenumber,score,examtime
要取出时间段中phonenumber的score最大并且examtime最小的记录,用max和min取的值都不是正确的记录值,用排序子查询的方法可以取到

select phonenumber,score,examtime,scoretime,createtime,rn from (
SELECT phonenumber,score,examtime,scoretime,createtime,ROW_NUMBER() OVER(PARTITION BY phonenumber ORDER BY score DESC,examtime) RN
FROM t_db_scores
WHERE to_date(scoretime,'yyyy-mm-dd hh24:mi:ss')>=to_date('2018-07-10 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_date(scoretime,'yyyy-mm-dd hh24:mi:ss')<=to_date('2018-07-20 23:59:59','yyyy-mm-dd hh24:mi:ss')
) where rn=1

先按score降序、examtime升序对phonenumber分组排序,再只取rn=1的记录,就可得到正确的结果

oracle 分组排序取出最大和最小的记录_升序