table : user
字段:uid begin end
select uid,sum(time) as all_time from (select uid, (case when max(end) over(partition by uid order by begin ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) is null then end -begin when max(end) over(partition by uid order by begin ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) >= end then 0 when begin > max(end) over(partition by uid order by begin ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) then end - begin else end - max(end) over(partition by uid order by begin ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ) END) as time from user) tmp group by uid