with a as ( select uid,in_time,row_number() over(partition by uid order by in_time) r1, row_number() over(partition by uid order by in_time desc) r2, datediff(max(in_time) over (order by in_time desc),in_time) cha from tb_user_log ), b as ( select uid, ...