sql分组后排序取第一条得写法

语法:row_number() over (partition by 分组字段 order by 排序字段 desc) row_id

select *
from (select row_number() over (partition by line_id order by completed_datetime desc) row_id,
             line_id,
             completed_datetime
      from patroltask_rec_cb
      where patrol_type_code = '01'
        and is_deleted = '01'
        and execute_state = 'M120205'
     ) patroltask_rec_cb
where row_id = 1;

你可能感兴趣的:(mysql,sql,mysql,数据库)