2

Sorry for the basicness of this query, but I need a bit of help learning here :)

This is a database for a Hotel.

Here are my tables:

  • Hotel (hotelNo, hotelName, city)
  • Room (roomNo, hotelNo, type, price)
  • Booking (hotelNo, guestNo, dateFrom, dateTo, roomNo)
  • Guest (guestNo, guestName, guestAddress)

Where the bolded are the primary keys. I realise it isn't the best design to have a concatenated primary key for Bookings, but that is the way it is here.

I need a query for the following:

How many different guests have made a booking for August?

6
  • 1
    what are the queries you tried so far, if any? It does not look like you tried anything? Commented Aug 7, 2014 at 10:22
  • Don't mind the composite primary key.. do mind the inconsistent use of 'ID' and 'No' however, especially on hotel and room. Commented Aug 7, 2014 at 10:25
  • That was my mistake, Arth. They are all No. I used ID on the first few out of habit. @TheChaos, I am not sure how to use joins... so I am stumped from the beginning.. I can do it if I can get the two tables joined properly Commented Aug 7, 2014 at 10:26
  • Ah ok, as long as they are all consistent now, please update the question. Commented Aug 7, 2014 at 10:28
  • Do you just want it for total guests regardless of hotel/room, and also do you just want the count or do you want the details for the guests? Commented Aug 7, 2014 at 10:29

6 Answers 6

1
SELECT COUNT (DISTINCT guestno)
FROM   booking
WHERE  MONTH(datefrom) = 8
Sign up to request clarification or add additional context in comments.

4 Comments

That will include any year, and won't include bookings that end in August but start before, or span August entirely.
This seems the easiest and simplest! Cheers mate
@Arth: Sorry, I should have specified more. I just need the bookings that have been made for August, as in dateFrom August.
@LukeBeacon No problem, I was working off "Just need a count of the unique guests at all hotels during August".. I'll leave my answer as is just in case you need that logic at some point.
1

Make a count of the number of guests during the month of July.

Then do the same for the month of August.

Make the difference between these two values ​​and have the number of new guests that have made a booking for August.

Not a very good shape, however it is an option where you can start.

1 Comment

Whaaat? Why even think about the July bookings.. i think he means distinct guests.
1

try like this

select guestName, guestAddress from Booking b 
inner join Guest g on b.guestNo=g.guestNo
inner join Hotel h on b.hotelNo=h.hotelID 
inner join Room r on b.roomNo=r.roomID
where b.hotelID=h.hotelID and 
MONTHNAME(dateFrom) = 'August' 
and MONTHNAME(dateTo)='August'

Comments

1

You don't actually need a JOIN:

SELECT COUNT(DISTINCT guestNo)
  FROM booking
 WHERE dateTo >= CONCAT(YEAR(CURDATE()),'-08');
   AND dateFrom < CONCAT(YEAR(CURDATE()),'-09');

This will include bookings that cover August entirely, or start or finish in August.. which I don't think the other answers cover.

I also assumed that you only wanted this for the current year.

Comments

0

Below query should work for you,

SELECT 
       Booking.*,
       Guest.*
FROM   Guest
       JOIN Booking 
              ON Guest.guestNo = Booking
       LEFT JOIN Room 
              ON Booking.roomNo = Room.roomID
       LEFT JOIN Hotel 
              ON Room.hotelID = Hotel.hotelID
WHERE
       MONTH(datefrom) = '8'

There is one more suggestion on booking table composite key. Composite key for Booking table should be guestNo, roomNo, hotelNo, dateFrom

Comments

0

For Postgres 15

/6.15 How many different guests have made bookings for August?/

select 
    count(distinct guestno) as "Number of different guests have made bookings for August"
from 
    booking
where 
    (extract (month from (datefrom)) >= 8 and extract (month from (dateto)) <=8 ) or 
    (extract (month from (datefrom)) <= 8 and extract (month from (dateto)) >=8 ) or
    (extract (month from (datefrom)) = 8) or 
    (extract (month from (dateto)) = 8)
;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.