I'm doing a job for the university where I have to do a travel management system, where I have tables such as Pasajeros
, Viaje
, Boleto
etc. Now I must make inquiries based on this basis that I created.
A particular query I could not make work, in this I have to list the data of the trains that have more than 20 seats which have not assigned any trip for tomorrow, which would be the column DATETIME FechaHoraViaje
.
DECLARE @Fecha DATE
SET @Fecha = DATEADD(DAY, +1, GETDATE())
SELECT t.IdTren,
t.TipoTren,
t.CapacidadTren,
t.MarcaTren,
CAST(v.FechaHoraViaje AS DATE)
FROM dbo.Tren AS t
JOIN dbo.Viaje AS v ON v.IdTren = t.IdTren
WHERE @Fecha IN (SELECT CAST(v.FechaHoraViaje AS DATE)
FROM dbo.Viaje AS v
WHERE CAST(v.FechaHoraViaje AS DATE) = '2017-06-08')
AND CapacidadTren > 20
GROUP BY t.IdTren,
t.TipoTren,
t.CapacidadTren,
t.MarcaTren,
CAST(v.FechaHoraViaje AS DATE)
In the part of the WHERE
use IN
to compare my variable @Fecha
with the result of the subquery, which would be exactly tomorrow's date.
The result should be the information of the only train that has no trips assigned for tomorrow, but instead it returns the information of both trains, I do not understand what is the logic to return both trains.