Come faccio ad ottimizzare questa select eliminando gli NOT EXISTS
select count(*),(select Immat from RIGA..Vehicule where IdVehicule=dv.IdVehicule) as targa,IdVehicule
from SPEITA..DestinationVehicule dv
where CodeTypeDestination ='LOG' and convert(datetime,DateFin,103) >'01/07/2010'
and not exists (select 1 from SPEITA..DestinationVehicule d2 where d2.IdVehicule=dv.IdVehicule and CodeTypeDestination<>'LOG')
and not exists (select 1 from SPEITA..DestinationVehicule d2 where d2.IdVehicule=dv.IdVehicule and DateFin is null )
group by IdVehicule
having count(*)=1

Rispondi quotando