Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables, Movies and ResrvationsOfMovies.
Movies has the column DirectorName
ReservationsOfMovies contains the columns ReservationStartDate and ReservationEndDate.

I want to select the DirectorName of all the movies, NOT INCLUDING those movies whose reservation was active a month ago.

What I have tried:

DECLARE @aMonthAgo DATE = CAST(ReservationEndDate - ReservationStartDate) AS DATE)

 SELECT movies.DirectorName
 FROM Movies movies
 INNER JOIN Reservations res ON res.IDMovies = b.IDMovies
 WHERE res.ReservationStartDate > @aMontAgo  AND res.ReservationEndDate < GETDATE();
Posted
Updated 4-Nov-20 2:34am

Forget the DECLARE, it will not work in the query, use the:
SQL Server DATEDIFF() Function[^]
 
Share this answer
 
SQL
DECLARE @Today date = GETDATE();
DECLARE @aMonthAgo date = DateAdd(month, -1, @Today);

SELECT movies.DirectorName
FROM Movies movies
INNER JOIN Reservations res ON res.IDMovies = b.IDMovies
WHERE res.ReservationStartDate > @aMonthAgo AND res.ReservationEndDate < @Today;
NB: Your date range might not be exactly what you want. If the reservation started more than a month ago, but doesn't end until tomorrow, that won't be counted. Perhaps you meant:
SQL
WHERE res.ReservationEndDate > @aMonthAgo AND res.ReservationStartDate < @Today;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900