select brand ,least(a.time_all,a.time_one) from (select brand ,min(start_time) as start_min ,max(end_time) as end_min ,max(end_time)-min(start_time)+1 as time_one ,sum(end_time-start_time+1) as time_all from t group by brand ) a