Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-18 : 05:50:11
|
| Hi Friends , I had two tables one Hotel Details and Other Hotel Booking DetailsTabel1-Hotel DetailsHaId Name Price1 MM 1002 KK 2503 SS 3004 ND 150Tabel2-Hotel Booking DetailsBId HaId FromDate ToDate 1 2 19/5/2009 21/5/20092 3 20/5/2009 25/5/20093 1 18/5/2009 19/5/2009When i search the dates From 21/5/2009 to 23/5/2009 ,here in table2 we can see that HaId - 2 and 3 are booked for the above dates from 21/5/2009 and 23/5/2009.But Haid 1 is booked fro some other date and Haid 4 in Table 1 are not even booked .So i need the details of HAid 1 and haid 4Please help me regarding this.Thanks in Advance |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-18 : 06:34:19
|
[code]DECLARE @Hotel TABLE( HaId int, Name varchar(10), Price int)INSERT INTO @HotelSELECT 1, 'MM', 100 UNION ALLSELECT 2, 'KK', 250 UNION ALLSELECT 3, 'SS', 300 UNION ALLSELECT 4, 'ND', 150DECLARE @Booking TABLE( BId int, HaId int, FromDate datetime, ToDate datetime)INSERT INTO @BookingSELECT 1, 2, '2009-05-19', '2009-05-21' UNION ALLSELECT 2, 3, '2009-05-20', '2009-05-25' UNION ALLSELECT 3, 1, '2009-05-18', '2009-05-19'DECLARE @bookfrom datetime, @bookto datetimeSELECT @bookfrom = '2009-05-21', @bookto = '2009-05-23'SELECT *FROM @Hotel hWHERE NOT EXISTS ( SELECT * FROM @Booking b WHERE b.HaId = h.HaId AND ( (b.FromDate <= @bookfrom AND b.ToDate >= @bookfrom) OR (b.FromDate <= @bookto AND b.ToDate >= @bookto) ) )/*HaId Name Price ----------- ---------- ----------- 1 MM 1004 ND 150(2 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-18 : 07:27:09
|
| Hi Friend Thank you for your reply.I got the answer with your query.Thanks once again |
 |
|
|
|
|
|
|
|