SQL sub query to retrieve total cost for each hotel - mysql

I'm trying to get my head around sub queries and I'm certain I don't actually know what I'm doing. I've got some code that pulls up the hotel names and room price, but the prices are showing the absolute total room price for all hotels together, not each hotel seperately.
SELECT hotelName, SUM(roomPrice) AS 'Room Price'
FROM hotel, room
GROUP BY hotelName
This code gives me this
2360 is the total room cost over every hotel, I just need to change it to show the cost of each hotels total rooms, individually.
EDIT: Added a image of the database relations

SELECT H.hotelName, SUM(R.roomPrice) as 'Room Price'
FROM hotel H
JOIN room R
ON H.hotelNo = R.hotelNo
GROUP BY H.hotelNo;

select SUM(b.roomPrice) AS 'Room Price' from hotel.hotel a, hotem.room b where a.hotelNo=b.hotelNo

Related

Aggregation using subquery: Error 1111 occurs. How do I fix it?

Please understand that I used Google Translator because I am not proficient in English.
First of all, I get error 1111 when using mysql.
The problem is to find the name of the customer whose average book table sales is higher than the average of each customer in the customer table.
Currently, the code I have written is:
1)
select customer.name from customer, Orders
where avg(Orders.saleprice) <
(select avg(Orders.saleprice) from Orders, customer
where Orders.custid = customer.custid AND Orders.custid = 1
group by orders.custid);
select customer.name from customer, Orders, book
where avg(Orders.saleprice) <
(select avg(Orders.saleprice) from Orders, customer
where Orders.custid = customer.custid group by orders.custid);
In the case of 2), I tried to compare values at a time, but the code failed because error 1111 occurred. In the case of 1), the code was written with only one custid set to '1'.
The picture above is the average of the purchase amount of each customer in each customer table.
The picture above is the average of the sales of the book table.
This is my first time using stackoverflow recommended by a subscribed YouTuber, so I am inexperienced and English is also inexperienced, but any help would be greatly appreciated.
You can solve the problem by next way:
-- select customers with average salesprice
select
customers.*,
avg(saleprice) as avgsaleprice
from customers
join orders on customers.id = orders.customer_id
group by customers.id, customers.name
-- filter result for customers having average greater then common order average
having avgsaleprice > (
select
avg(saleprice) as avgsaleprice
from orders
);
Look the fiddle at SQLize.online

SUM and GROUP BY Using a Subquery

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

Subquery and SUM

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;

Left outer join in rails

I have two objects - Customers & Customer Checkins. Each customer has many checkins and each checkin belongs to one customer.
I need to come up with a query that outputs the complete list of customers together with their checkin count per month. My current query is shown below:
customer = Customer.where(account_id: 139).joins('LEFT JOIN
customer_checkins on customer_checkins.customer_id =
customers.id').uniq.select("COUNT(*) as count, customers.created_at as
created_at, customers.name, customers.telephone, customers.mobile,
customers.email, customers.gender, customers.city,
customers.birthday,date_format(customer_checkins.created_at, '%b') as
'month', customers.id").group("customer_checkins.customer_id,
date_format(customer_checkins.created_at, '%b %Y')")
This query does not list all the customers. What is wrong with my code?
It does not list all the customers because you're only searching for ones with account_id: 139.
Replace
customer = Customer.where(account_id: 139)...
With
customer = Customer...

MySQL Complex Queries

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.