MySQL Forums
Forum List  »  Newbie

Re: Query problem
Posted by: Bill Karwin
Date: April 06, 2006 07:23PM

Here's a solution that seems to work. This sort of problem isn't easy.

CREATE TABLE `rents` (
`customerid` int(11) default NULL,
`startdate` date default NULL,
`enddate` date default NULL,
`vehicleid` int(11) default NULL
)

INSERT INTO rents VALUES
(2, '2006-04-04', '2006-04-11', 6),
(3, '2006-04-06', '2006-04-30', 6),
(4, '2006-04-04', '2006-04-11', 8);

select r2.customerid, r2.vehicleid, r2.startdate as s2, r1.startdate as s1
from rents as r1 inner join rents as r2
on (r1.vehicleid = r2.vehicleid and r1.customerid <> r2.customerid
and (r1.startdate > r2.startdate and r1.startdate <= r2.enddate))
union all
select r2.customerid, r2.vehicleid, r1.enddate as s2, r2.enddate as s1
from rents as r1 inner join rents as r2
on (r1.vehicleid = r2.vehicleid and r1.customerid <> r2.customerid
and (r1.enddate < r2.enddate and r1.enddate >= r2.startdate))
union all
select r1.customerid, r1.vehicleid, r1.startdate, r1.enddate
from rents as r1 left outer join rents as r2
on (r1.vehicleid = r2.vehicleid and r1.customerid <> r2.customerid
and (r1.startdate between r2.startdate and r2.enddate or r1.enddate between r2.startdate and r2.enddate))
where r2.customerid is null

Regards,
Bill K.

Options: ReplyQuote


Subject
Written By
Posted
April 05, 2006 12:40PM
Re: Query problem
April 06, 2006 07:23PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.