简体   繁体   中英

Combining SQL Query Across Multiple Tables

I'm looking to list the Hotel_ID, Hotel_Name, and total number of rooms booked for all hotels. However, I only need to show those with more than 8 rooms booked and purchased by Gold class customers. I am brand new to SQL (started about a day ago), so please bear with me in my mistakes.

Here is what I have been trying, which will show the Hotel ID/Name/total number if ALL of the rooms booked for that hotel were by Gold class guests, but if the there is a mix (eg some rooms/bookings were booked by Gold class, others by Silver), then that hotel will not show up.

(eg If Hilton had a total of 12 rooms booked, all by Gold members under various booking ID's, it will show Hilton, [Hotel ID], 12. But if Super 8 had a total of 10 rooms booked by Gold members and another 7 booked by Silver, then it SHOULD show Super 8, [Hotel_ID], 10 - The way I have it now, Hilton would work but the Super 8 example wouldn't show up at all).

SELECT hotel.hotel_id, hotel.hotel_name, SUM(rooms.number_rooms) AS total_rooms_booked
FROM hotel, rooms
WHERE hotel.hotel_id=rooms.hotel_id
HAVING SUM(rooms.number_rooms) > 8
GROUP BY hotel.hotel_id, hotel.hotel_name
INTERSECT
SELECT hotel.hotel_id, hotel.hotel_name, SUM(rooms.number_rooms) AS total_rooms_booked
FROM hotel, rooms, booking, guests
WHERE hotel.hotel_id=rooms.hotel_id AND rooms.booking_id=booking.booking_id
AND booking.guest_id=guests.guest_id AND class='gold'
GROUP BY hotel.hotel_id, hotel.hotel_name
;

Here is how the tables are set up.

HOTEL
Hotel_ID (1,2,3,4,etc.)
Hotel_Name (Hilton, Marriott, Ritz, Super 8, etc.)

ROOMS
Booking_ID (101, 102, 103, 104, etc.)
Hotel_ID (see above)
Number_Rooms (however many rooms were booked by the guest under a specific Booking_ID/Date)

BOOKING
Booking_ID (see above)
Booking_Date (DD-MM-YY)
Guest_ID (1,2,3,4, etc.)

GUESTS
Guest_ID (see above)
Guest_Name (first last)
Class (Bronze, Silver, Gold)

If each hotel needs at least 8 rooms booked by "Gold" guests, and you only want to count the "Gold" bookings (not any others), then the JOIN s below are all you need:

SELECT hotel.hotel_id, hotel.hotel_name, 
SUM(Rooms.Number_Rooms) AS total_rooms_booked
FROM hotel
INNER JOIN rooms ON hotel.hotel_id=rooms.hotel_id
INNER JOIN bookings on rooms.booking_id = bookings.booking_id
INNER JOIN guests on bookings.guest_id = guests.guest_id 
    AND guests.class = 'Gold'
GROUP BY hotel.hotel_id, hotel.hotel_name
HAVING SUM(Rooms.Number_Rooms) > 8
ORDER BY hotel.hotel_id, hotel.hotel_name

Your expected output is confusing since you mention total rooms booked to count also rooms booked by non-gold members but then again in the output you mention that the count should only involve gold member's bookings. I'm assuming the latter:

select
  h.hotel_id,
  h.hotel_name,
  sum(r.number_rooms) AS total_rooms_booked
from hotel h
join rooms r on h.hotel_id = r.hotel_id
join booking b on b.booking_id = r.booking_id
join guests g on g.guest_id = b.guest_id
where 
  g.class = 'gold'
group by
  h.hotel_id, h.hotel_name
having
  sum(r.number_rooms) > 8

Please use explicit JOIN syntax instead of the old-fashioned joins in WHERE clause.

Also, your model seems like it needs improvements. Why would you involve booking_id column within the rooms table? The entire purpose of booking and rooms seems a little vague. This could be done with rooms table presenting the rooms existing within hotels and the bookings table which will effectively mark the time of arrival and departure of your guests (reservation) of rooms within hotels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM