Создать простую таблицу
declare @simple table
(
order_id int,
movie_id int,
rent_date date,
return_date date
)
И некоторые простые данные
insert into @simple values
(1, 1, '2014-07-17', NULL),
(2, 1, '2014-07-18', NULL),
(3, 1, '2014-07-19', '2014-07-17'),
(4, 2, '2014-07-17', '2014-07-18'),
(5, 2, '2014-07-17', '2014-07-18'),
(6, 3, '2014-07-17', NULL),
(7, 3, '2014-07-18', '2014-07-19')
запрос
select distinct movie_id
from @simple t
where exists -- Rule 1
(
select rent_date
from @simple x
where return_date is not null
and movie_id = t.movie_id
group by rent_date
having count(*) = 2
)
or exists -- Rule 2
(
select *
from @simple x
where x.movie_id = t.movie_id
and x.rent_date = t.return_date
)