SQL problem

Status
Not open for further replies.

kleel

Ars Scholae Palatinae
1,022
I'm stumped. the db schema involved looks like this...
4 tables as follows:

Hotels
------
ID
Name

|
|
8

HotelBooking
-------------
ID
HotelID

|
|
8

GuestLink
---------
ID
HotelBookingID
GUESTID

8
|
|

Guests
------
ID
FName
LName


I need to get a record set of all the bookings at a particular hotel.
which seems is straight forward enough until you realise that one or more guests can be sharing a booking (e.g. husband & wife) and I need to list them too (comma seperated)!


any ideas? my brain is hurting :confused:


K
 

kleel

Ars Scholae Palatinae
1,022
Originally posted by reboot:
select distinct bookings, ...
group by lname
I tried that ... which worked when I didnt include the guest names .. when I do the result is a new row for each guest which is not what I want as the report is going to be used as a check list for each hotel and needs to show each booking as upposed to each guest.
I'm not sure what you mean with the "|"s and "8"s
the "|"s and "8"s are "one to many" relationships - I didnt know how else to type them, sorry for the confusion.
 

bigterp

Ars Tribunus Militum
1,576
So for the query it looks like there's a one to many relationship between Hotel Bookings and Guests, meaning each hotel booking can have one or more guests. What happens if there's more than two guests for a booking?

EDIT:
You really can't produce a comma delimited list from a single SQL query when you don't know how many names can be related to a booking. That's more of a report program task.

EDIT:

However you can produce a list of names for each Bookings by grouping by Hotel and Bookings.

Select h.name, b.id, g.Fname, g,Lname
from hotel h, Hotelbooking b, guests g, GuestLink as GL,
where h.id = b.hotelid
and b.id = gl.hotelbookingid
and gl.guestid = g.id
group by h.name, b.id

[This message was edited by bigterp on February 05, 2002 at 11:24.]
 

kleel

Ars Scholae Palatinae
1,022
damn I thought that was going to be the case .. just was in denial I guess ...

Would putting the query to find each booking for a hotel into a cursor and looping thru adding the booking data & all the guests into a temp table and returning that work?

are cursors a bad idea?

This to be done in a Stored Procedure .. as I'm using crystal reports 8.5 for the reporting and I dont trust it's SQL builder, in fact I dont trust it at all :)
 

kleel

Ars Scholae Palatinae
1,022
ok I have it ! sorry if replying to your post is frowned apon .. I just need to share :D

The solution is a bit lengthy so I'll summerise.

one main booking_cursor, looping around all the bookings, for each booking a test to see if a booking is a sharing one or not is made.

If it is then another sharing_cursor is declared, opened and looped thru, concat'ing all the sharing guests names into a var. and deallocating the cursor.

If not then it just selects the guest involved into the same var as above.

The main cursor then selects all the values retrived and loops.

Nesting cursors like this scares me though the amount of times this will happen will be negliagable, plus the odds of more than 2 guests shareing is even rarer.
 

bigterp

Ars Tribunus Militum
1,576
Actually it would be easier if you always got the customer cursor:

For a booking open the cursor
If the cursor has data
Write the names of the first customer
advance the cursor
while the cursor still has customers
Write a comma
Write the current customer names
advance the cursor
end while
end if
close cursor

Thereby avoiding multiple selects, and the corresponding maintenace overhead.
 
Status
Not open for further replies.