sql >> Base de Datos >  >> RDS >> Mysql

2 contar(*)+agrupar por+tener+unirse

¿No proporcionaría esta consulta el resultado deseado?

select sum(un_ter), sum(u_t_a) from (
select 
count(distinct ut.tr_id) as un_ter, 
count(case when ut.ev_t = 'accident' then 1 end) as u_t_a
from un_tr ut
left join un_pl up ON  (ut.s_p_c = up.coo) or  (ut.d_c = up.coo) 
where up.gal = 'Zero' and date between '2018-06-01' and '2018-06-31'
group by ut.tr_id
having count(ut.ut_id)>1
) t1;