Hola a todos,
Tengo un problema con una disponibilidad de vehiculos con las siguientes
tablas
tblAlquilerVehiculo
(
[id] [int] IDENTITY (1, 1) NOT NULL ,
[fecha] [datetime] NOT NULL ,
[idVehiculo] [int] NOT NULL ,
[fechaSalida] [datetime] NOT NULL ,
[fechaEntrada] [datetime] NULL ,
[reserva] [bit] NOT NULL ,
)
[tblVehiculo
(
[id] [int] IDENTITY (1, 1) NOT NULL ,
[matricula] [varchar] (10) COLLATE Modern_Spanish_CI_AS NOT NULL ,
[modelo] [varchar] (50) COLLATE Modern_Spanish_CI_AS NOT NULL ,
)
En tblVehiculo aparecen todos los vehiculos que existen, y en
tblAlquilerVehiculo todos los alquileres que se han realizado.
Necesito saber dado una fechaDisponibilidad, todos los vehiculos que estén
disponibles para alquiler. Teniendo en cuenta los vehiculos cuya
fechaEntrada (cuando lo devuelven) tienes que ser menor o igual a la
fechaDisponibilidad, y resaltar que si un no tiene alquier (no ha salido
nunca) también debe aparecer.
Se me ha ocurrido hacer una select con not exist de la sigueinte manera...
pero no me está resultando correctamente.
select distinct veh.[id], veh.[matricula], veh.[modelo]
from tblVehiculo as veh,
tblAlquilerVehiculo as alq
where not exists
(
select *
from tblAlquilerVehiculo as alq
where alq.[idVehiculo] = veh.[id]
)
or
(
select max(fechaEntrada)
from tblAlquilerVehiculo as alq where
alq.[idVehiculo] = veh.[id]
and alq.fechaSalida < '07/05/2005'
group by alq.[idVehiculo]
having max(fechaEntrada) > max(fechaSalida)
) <= '07/05/2005'
and alq.[idVehiculo] = veh.[id]
Alguien que me pueda ayudar? muchas gracias por vuestro tiempo
- Javier Pérez
Leer las respuestas