Essayez cette version :
select contract_no AS c_no, cm_mac AS c_mc, min(tstamp) as time2, count(*) as aps
from devices d
where exists (select 1
from devices d2
where d2.contract_no = d.contract_no and
tstamp >= '2018-10-28 06:59:59' and
tstamp <= '2018-10-29 07:00:00'
)
group by contract_no, cm_mac;
Vous voulez un index sur devices(contract-no, tstamp)
.