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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 problems selecting the correct data subset

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-09-27 : 09:16:39
Tallal writes "I'm tearing my hair out over this, I'm sure that I've overlooked
something really obvious and if anyone can help I would be eternaly greatful.
The DB is MSSQL7SP3 on NT4sp6a .

The Database
============
2 tables involved, one is a Delegate table the other is a DelegateHotelBooking Table.
A delegate can have any number of hotel bookings, and (here's the killer) can also share any number of hotel bookings with other delegates.
To put it another way - a hotel booking is a room booked in a hotel for either one or two delegates in a particular time period.

Requirement
===========
I need a SELECT statement that returns the following:

[]all delegates who have no bookings
[]delegates who have a booking/s but none fall into the period @STARTDATE, @ENDDATE
[]delegates who are sharing a booking/s but none fall into the period @STARTDATE, @ENDDATE

Table Defs
==========



Create Table dbo.Delegate

(

DelegateID int Not Null Identity (1, 1),

Title varchar(10) Null,

FirstName varchar(50) Not Null,

LastName varchar(50) Not Null

/* SNIP cols that are not relavent */

)

Go

Alter Table dbo.Delegate Add Constraint

PK_Delegate Primary Key Nonclustered

(

DelegateID

)

Go

/*------------------------------------------------------------------*/

Create Table dbo.DelegateHotelBooking

(

BookingID int Not Null Identity (1, 1),

DelegateID int Not Null,

ShareDelID int Null,

ChkIn datetime Not Null,

ChkOut datetime Not Null

/* SNIP cols that are not relavent */

)

Go

Alter Table dbo.DelegateHotelBooking Add Constraint

PK_DelegateHotelBooking Primary Key Nonclustered

(

BookingID

)

Go

Alter Table dbo.DelegateHotelBooking Add Constraint

FK_DelegateHotelBooking_Delegate Foreign Key

(

DelegateID

) References dbo.Delegate

(

DelegateID

)

/*----------------------------------------------------------------*/






QUERY
=====
My best attempt at getting the records so far works fine if a delegate is only
sharing one other room or has one other bookling.
If they have multiple booking the result set is incorrect tho



Alter Procedure get_sharingDelegates

(

@Sdate as datetime,

@Edate as datetime

)

As

/********************************************/

/*ALL DELS SHARING BUT NOT DURING THE PERIOD*/

SELECT Delegate.DelegateID, Delegate.Title+'
'+Delegate.FirstName+' '+Delegate.LastName as FullName

FROM Delegate INNER JOIN

DelegateHotelBooking ON

Delegate.DelegateID = DelegateHotelBooking.ShareDelID

WHERE

DelegateHotelBooking.ChkIn not between convert(datetime,@Sdate,103)
and convert(datetime,@Edate,103) and

DelegateHotelBooking.ChkOut not between
convert(datetime,@Sdate,103) and convert(datetime,@Edate,103)

UNION




/************************************************************/

/*ALL DELS WHO HAVE HOTEL BOOKINGS BUT NOT DURING THE PERIOD*/



SELECT Delegate.DelegateID, Delegate.Title+'
'+Delegate.FirstName+' '+Delegate.LastName as FullName

FROM Delegate INNER JOIN

DelegateHotelBooking ON

Delegate.DelegateID = DelegateHotelBooking.DelegateID

WHERE

DelegateHotelBooking.ChkIn not between

convert(datetime,@Sdate,103) and convert(datetime,@Edate,103)

and

DelegateHotelBooking.ChkOut not between

convert(datetime,@Sdate,103) and convert(datetime,@Edate,103)

UNION



/****************************************/

/*ALL DELS WHO DONT HAVE HOTEL BOOKINGS */



SELECT Delegate.DelegateID, Delegat

   

- Advertisement -