In need of help i have a question SQL statement that retrieves the maximum "Total Room Cost" in a hotel chain. And use a subquery to determine the total room cost grouped by hotel.. I can sort of do the 2 querys but i keep getting errors putting them together. my two tables. hotel--with table hotel_no and hotelname. room--with table room_no,hotel_no,room_type,room_price
So far i have
SELECT hotelname, SUM(room_price) as Total
FROM hotel, room
WHERE hotel.hotel_no = room.hotel_no
GROUP BY Hotelname
Gives me Hotel name and each hotel price
and
SELECT SUM(room_price) AS Total
FROM room
Gives me Total
every time i try to put them together i get an error
eg
SELECT hotel_no, SUM(room_price) AS "Total"
FROM room
WHERE hotel_no= hotel_no
UNION ALL
SELECT hotel_no, hotelname
FROM hotel
WHERE hotelname = hotelname
group by hotelname;
Please help Thx
Sry to be a pain but Apparently it got the right answer but i was wrong it has to be a subquery in the from
Question at this Hotel, every hotel in the chain has numerous rooms at various costs, The Hotel wants to know which hotel has the highest room cost total
Sample
SELECT MAX(SubFromName.NewColumnName)
FROM (SELECT columnName, SUM(columnName) AS ‘NewColumnName’
FROM table
GROUP BY columnName) SubFromName;
Hope this link works this is the schema data http://www.sqlfiddle.com/#!9/48429
The closest i have is below but still not right
select hotelname, MAX(room_price), SUM(room_price) AS 'Total Room Cost'
from room,hotel
WHERE hotelname IN (SELECT hotelname FROM hotel
WHERE hotel.hotel_no = room.hotel_no)
group by hotelname;
Cant have join or union should be like the sample If anyone gets what i mean Thankyou in advance. Im having a brain dead week.
Select chain_name, sum(price)
From chain a
Inner join
(Select a.hotel_no, sum(room_price) as price, b.chainid
From room a
Inner join hotel b
On a.hotel_no = b.hotel_no
Group by a.hotel_no) b
On a.Id =b.chainid
Group by chain_name
Try this:
SELECT hotel_no, SUM(room_price) AS Total, "" as hotelname
FROM room
WHERE hotel_no= hotel_no
UNION ALL
SELECT hotel_no, "" AS Total, hotelname as hotelname
FROM hotel
WHERE hotelname = hotelname
group by hotelname;
UNION ALL should have same number of columns in both queries.
Just to answer myself this is what i ended up with which worked
SELECT hotelname,
(SELECT SUM(room_price) from room where hotel_no = hotel.hotel_no) as TotalRoomCost
from hotel
GROUP BY hotelname
it was ment to be this
SELECT MAX(Eagle.TotalRoomCost)
FROM (SELECT hotel_no, SUM(room_price) AS 'TotalRoomCost' FROM room GROUP BY hotel_no) Eagle;
Related
I am confused on which table I should use and or should I join the tables when attempting this question?
List total number of hotels in the database that have less than 10 rooms.
Hotel (hotelNo, hotelName, city)
Room (roomNo, hotelNo, type, price)
Booking (hotelNo, guestNo, dateFrom, dateTo, roomNo)
Guest (guestNo, guestName, guestAddress)
I have tried by just useing one table Room in my statement
SELECT hotelNo
FROM Room
WHERE roomNo < 10
GROUP BY hotelNo;
Would this be correct or should I use something like this?
SELECT h.hotelNo,r.roomNo
FROM Hotel h JOIN Room r ON h.hotelNo= r.hotelNo
WHERE r.roomNo < 10
GROUP BY hotelNo;
Assuming that all hotels have at least one room, you don't need a join. But, you do need aggregation:
select count(*)
from (select r.hotelno
from rooms r
group by r.hotelno
having count(*) < 10
) r;
The subquery returns the hotels that have fewer than 10 rooms (and are in the rooms table, so they have at least one room).
The outer query counts the number of such hotels.
Check count on the having in order to be applied to the groups, and that count the number of rows returned by that query
SELECT COUNT(*)
FROM (
SELECT hotelRo
FROM Room
GROUP BY hotelNo
HAVING COUNT(*)<10
) AS TMP;
I've got the following tables:
Employee (#PNo, Name, *ANo, Salary)
Department (#ANo, AName)
Hotel (#HNo, HName, HCategory, ZIP, City)
Journey (#*Employee, #*Hotel, #BeginningDate, Duration, Costs)
(#=primary key, *=foreign key)
I want to delete all hotels which have been booked with a maximum of one stay and tried it with
DELETE hotel FROM hotel
INNER JOIN journey ON journey.Hotel = hotel.HNo
WHERE COUNT(journey.Hotel) < 2;
But that doesn't work. All I get is the following error:
"#1111 - Invalid use of group function"
How can I connect the two tables and delete the hotels in the table "hotel" that were not booked more than ones?
ANSI SQL complaint answer:
DELETE hotel FROM hotel
WHERE HNo not in (select Hotel
from journey
group by Hotel
having count(*) >= 2)
The sub-query will return Hotels that exist more than once in the journey table.
Use LEFT JOIN to join hotel with journey and count any column from journey table. This will return hotels + journey count, including hotels with zero journeys.
DELETE FROM hotel
WHERE HNo IN (SELECT * FROM (
SELECT hotel.HNo
FROM hotel
LEFT JOIN journey ON hotel.HNo = journey.Hotel
GROUP BY hotel.HNo
HAVING COUNT(journey.Hotel) < 2
) AS foobar)
The sub-sub-query trick is described here.
Try this
DELETE FROM hotel
WHERE HNo IN (SELECT hotel.HNo
FROM hotel
JOIN journey on (journey.hotel = hotel.HNo)
GROUP BY hotel.HNo
HAVING count(hotel.HNo) < 2)
DELETE FROM hotel WHERE secnum IN (SELECT secnum FROM journey GROUP BY secnum HAVING COUNT(*) > 2 )
I been working on a question given to me to find the total room price of a hotel chain using a subquery and grouping them by Hotel. I currently have the following which seems to not work at all and get an error.
SELECT MAX(roomPrice) AS 'Total Room Price'
FROM room
WHERE IN
(SELECT roomPrice, SUM(roomPrice) AS 'TotalRoomCost' FROM room GROUP BY hotel);
Any help would be appreciated
Screenshot of the Tables
Simple fix is to select from the sub query.
I think from your description you want to sum all the room prices for each hotel, and the get the max total room price from all the individual hotels. If so something like this:-
SELECT MAX(TotalRoomCost ) AS 'Total Room Price'
FROM
(
SELECT hotel, SUM(roomPrice) AS TotalRoomCost FROM room GROUP BY hotel
) sub0;
Please change column names as these are not shown in attached screenshot.
SELECT
H.Name AS HotelName,
SUM(R.roomPrice) AS 'Total Room Price'
FROM room AS R
INNER JOIN hotel AS H
ON R.HotelID=H.HotelID
GROUP BY H.Name
Hey I've been killing myself trying to figure out how to do these queries. Can someone help me out.
These are the tables I have currently.
BOOKING
HOTEL_NO
GUEST_NO
DATE_FROM
DATE_TO
ROOM_NO
GUEST
GUEST_NO
GUEST_NAME
CITY
ADDRESS
ZIP_CODE
HOTEL
HOTEL_NO
HOTEL_NAME
CITY
ADDRESS
ZIP_CODE
STAR
ROOM
ROOM_NO
HOTEL_NO
ROOM_TYPE
PRICE
And these are the queries I need to do.
-List the guests that have all their bookings (past and present) in the same hotel.
-Create a view VIP-Guest that lists guests who have reservations for only 4 star hotels or
4 star hotels
-Among the VIPs find the guest with the largest total stay (in term of number of days).
Express this as a query with the view and without the view
Can someone help me out?
this should get you started. to post on stackoverflow, you need to come with specific questions or errors or problems. like for the query you posted in the comments up top.... that could be a question in itself: "I have these tables, this one specific goal (question/result set), and I tried this query... it gives me this result or it gives me this error."
BOOKING: HOTEL_NO, GUEST_NO, DATE_FROM, DATE_TO, ROOM_NO
GUEST: GUEST_NO, GUEST_NAME, CITY, ADDRESS, ZIP_CODE
HOTEL: HOTEL_NO, HOTEL_NAME, CITY, ADDRESS, ZIP_CODE, STAR
ROOM: ROOM_NO, HOTEL_NO, ROOM_TYPE, PRICE
all guests and bookings...
-- all guests: select * from guest;
-- all bookings: select * from booking;
select *
from guest
join booking on guest.guest_no = booking.guest_no;
-- which is the same as...
select *
from guest, booking
where guest.guest_no = booking.guest_no;
-- and... your comments query was missing a group by clause
select guest_no, guest_name, count(*) as booking_count
from guest
join booking on guest.guest_no = booking.guest_no
group by guest_no, guest_name;
select guest_no, guest_name, count(distinct hotel_no) as hotel_count
from guest
join booking on guest.guest_no = booking.guest_no
group by guest_no, guest_name
having count(distinct hotel_no) = 1;
and I count(distinct hotel_no) because... they might have 3 bookings at Hotel A and 1 at Hotel B. The basic join would give me 4 rows for that person. I don't care how many bookings. I care how many hotels. So I want to count the distinct occurrences of hotel_no per person (there's that group by) instead of every row.
guests by their stars...
-- so we have to get guest and hotel joined. bc hotel has stars.
-- booking has hotel_no. so... we can use that last query and
-- join in HOTEL to get the star information. in the WHERE you
-- will want to put your filter for the number of stars that you
-- are looking for =4 or >=4 or something like that.
-- you might want to check out DISTINCT to get just a list of names
-- instead of a row for each booking.
number of days they stayed...
-- use the second query.
-- datediff(date_to, date_from) as days_stay gives you the length of stay
-- i don't know what the view is.
-- to get the top length could go two ways... either ORDER BY and LIMIT if there is
-- only one person with the top length (let's say 10 days). if there are many people
-- who have stayed 10 days, you'll need to do a MAX on the days_stay and either join
-- that in or use it in the WHERE as a nested select.
this assumes there is a single highest length of stay. only one person stayed 10 days.
SELECT guest_no, guest_name, datediff(date_to, date_from) days_stayed
FROM vip_guest
join booking on vip_guest.guest_no = booking.guest_no
order by datediff(date_to, date_from) desc
limit 1,1
this should work for many... (i'm not testing these... just kind of looking at it)
SELECT distinct guest_no, guest_name, datediff(date_to, date_from) max_stay
FROM vip_guest
join booking on vip_guest.guest_no = booking.guest_no
where datediff(date_to, date_from) = (
select max(datediff(date_to, date_from)) as days_stayed
from booking )
the nested query gets the maximum stay length of everyone. vip_guest and bookings joined together give us guest and date imfo. we will get all bookings for every vip_guest. so we want to filter it down to where stay lengths == the max stay length. in case a person had multiple 10 day stays (my arbitrary max stay length)... use distinct.
now... thats a good point about the nested query. i don't know what is in your view. it is possible none of the max vip guests had a stay as long as the max stay length. in that case, this query would return nothing.
I am trying to retrieve names and address of all guests with bookings for a hotel in London, alphabetically ordered by name in MySQL using subqueries and getting this Error:
Error Code: 1242. Subquery returns more than 1 row
Here's the query that I run:
select * from guest
where guest_no =
(
select guest_no
from booking
where hotel_no = (select hotel_no
from hotel
where city = 'London')
);
and here's the schema for hotel, booking and guest:
hotel (hotel_no, hotel_name, city)
booking (hotel_no, guest_no, date_from, date_to, room_no)
guest (guest_no, g_name, g_address)
additionally, here's the schema for room:
room (room_no, hotel_no, type, price)
Please help me with the above mentioned error and possible solutions.
Thanks and regards.
why not use join as
select
g.guest_no,
g.g_name,
g.g_address
from guest g
inner join booking b on b.guest_no = g.guest_no
inner join hotel h on h.hotel_no = b.hotel_no
where h.city = 'London'
When you use '=', it means that the result of your subquery is exactly 1 row. If you expect multiple results, you need to use the IN keyword, like so:
select * from guest where guest_no IN (select guest_no from booking where hotel_no IN (select hotel_no from hotel where city = 'London'));
EDIT: As #flaschenpost mentions, the performance could be degraded in case there is no proper indexing on the columns involved in the subqueries. You would probably do well to use JOIN rather than such nested subqueries.
Change you query to
select * from guest
where guest_no IN
(select guest_no from booking where hotel_no
IN (select hotel_no from hotel where city = 'London'));
you need joins !
try this !
select a.g_name,a.g_address from guest a inner join booking b on a.guest_no=b.guest_number inner join hotel h on b.hotel_no=h.hotel_no inner join rooms r on b.room_no=h.room_no
where h.city='London'