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
Related
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
Goal. To make a query that displays Product Name(Products), Product Type(ProductTypes), and total number of sales of each product(Sales)
Here are my tables:
I am having real difficulty figuring out how I am meant to do this. I am trying to do a UNION and a few other things but cannot get it to work.
I can get the total number of sales by using this SELECT ProductID, count(*) as NumSales from Sales group by ProductID but really struggling to do the rest and format it correctly. Any help would be appreciated.
EDIT:
Select Products.ProductName, ProductTypes.ProductType
From Products
INNER JOIN ProductTypes ON Products.ProductTypeID=ProductTypes.ProductTypeID
I have this to display this right now, just need to join the sales count somehow.
try:
select prod.ProductName, ptyp.ProductType, count(SaleID) count_sale
from Products prod
join ProductTypes ptyp on ( ptyp.ProductTypeID= prod.ProductTypeID)
join Sales sal on ( sal.ProductID = prod.ProductID)
group by prod.ProductName, ptyp.ProductType
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
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;
I need to find the average price of a movie by genre.
The tables are Movie (movie_genre) and Price (price_rentfee)
I tried:
select movie_genre, avg(price_rentfee)
from movie, price
group by movie_genre;
It lists the movie's by genre with the avg rental fee,
but the average rental fee is the same for all of them.
Is there a way where I can average it out by genre?
Your query says
FROM movie, price
That's probably a mistake. It generates every possible combination of movie and price. You probably need something like this instead to get useful results.
FROM movie
JOIN price ON movie.movie_id = price.movie_id
your query need key column to join both of tables.
select movie_genre, avg(price_rentfee) as avgPrice
from movie, price
where movie.movie_id = price.movie_id
group by movie_genre;
or
select movie_genre, avg(price_rentfee) as avgPrice
from movie
Left Join price
on price.movie_id = movie.movie_id
group by movie_genre;
Turns out I was missing a join, group by, and having clause. Answer i was looking for was
select movie_genre, avg(price_rentfee)
from price
right join movie
on price.price_code = movie.price_code group by movie_genre;
Sorry for the ambiguous question and thanks for the help.