select t.*, ROUND((avgNum/greaterNum)/60.0,2) avgTime from (SELECT
( SELECT count(1) from (
SELECT
t1.sequence_nbr
from tz_alert_called t1 where t1.region_code like concat('%',#{regionCode}, '%') and t1.alarm_type_code = '960' and call_time BETWEEN #{startDate} and #{endDate} and father_alert is null
) as t1sn
) as trappedPeople,
( SELECT count(1) from (
SELECT
t1.sequence_nbr
from tz_alert_called t1 where t1.region_code like concat('%',#{regionCode}, '%') and t1.alarm_type_code = '961' and call_time BETWEEN #{startDate} and #{endDate} and father_alert is null
) as n
) as breakdownRescue,
( SELECT count(1) from (
SELECT
t1.sequence_nbr
from tz_alert_called t1 where t1.region_code like concat('%',#{regionCode}, '%') and t1.alarm_type_code = '962' and call_time BETWEEN #{startDate} and #{endDate} and father_alert is null
) as t1sn2
) as complaint,
(SELECT sum( field_value ) FROM tz_alert_form_value WHERE alert_called_id IN (SELECT sequence_nbr FROM tz_alert_called WHERE region_code LIKE concat ('%', #{regionCode}, '%' ) AND call_time BETWEEN #{startDate} and #{endDate} AND alarm_type_code = '960' AND father_alert IS NULL) AND field_code = 'trapped_num') as rescuedCount,
( SELECT count(1) from (
SELECT
t1.sequence_nbr,
(SELECT min(arrive_time) from tz_dispatch_task where alert_id = t1.sequence_nbr and arrive_time is not null) as arr,
(SELECT min(dispatch_time) from tz_dispatch_task where alert_id = t1.sequence_nbr and dispatch_time is not null ) as dis
from tz_alert_called t1 where t1.region_code like concat('%', #{regionCode}, '%') and t1.alarm_type_code = '960' and call_time BETWEEN #{startDate} and #{endDate} and father_alert is null
) as snad where extract(epoch from arr - dis ) > 1800
) as withinThirtyRescue,
(SELECT count(1) from tz_alert_called where region_code like concat('%', #{regionCode}, '%') and alarm_type_code = '960' and call_time BETWEEN #{startDate} and #{endDate} and father_alert is null) as greaterNum,
(
SELECT sum(extract(epoch from arr - dis )) from
( SELECT
t1.sequence_nbr,
(SELECT min(arrive_time) from tz_dispatch_task where alert_id = t1.sequence_nbr and arrive_time is not null) as arr,
(SELECT min(dispatch_time) from tz_dispatch_task where alert_id = t1.sequence_nbr and dispatch_time is not null ) as dis
from tz_alert_called t1 where t1.region_code like concat('%', #{regionCode}, '%') and t1.alarm_type_code = '960' and t1.call_time BETWEEN #{startDate} and #{endDate} and t1.father_alert is null