我试图执行一个查询这给了我最盈利空间的总和在每家酒店(25家)
下面是我的查询:
SELECT hotels.hotel_id,rooms.room_id,hotel_name,room_number,sum(rooms.room_price) AS profit,COUNT(rooms.room_id) AS count FROM hotels,rooms,bookings WHERE hotels.hotel_id=rooms.hotel_id AND rooms.room_id=bookings.room_id GROUP BY rooms.room_id
这是我得到的最接近的结果..忽略了酒店名称的语言
这是我迄今达成的结果,
酒店
客房
预订第1页
预订第2页(其余记录)
HOTEL_ID 1有5间房间,300做出了最盈利的room_number。 我想展现的淋漓尽致利润只有每家酒店的。 我不需要这样做利润少了其他房间。
更新:所以,我解决,我想获得最好的2室,做了最盈利的类似查询。 但是,我只是不能认为可以给我每家酒店的唯一最好的任何利润的功能。 小提示或帮助将appriciated
试试这个查询:
SELECT * FROM
(SELECT hotels.hotel_id,rooms.room_id,hotel_name,room_number,SUM(rooms.room_price) AS profit,COUNT(rooms.room_id) AS COUNT
FROM hotels,rooms,bookings
WHERE hotels.hotel_id=rooms.hotel_id
AND rooms.room_id=bookings.room_id
GROUP BY rooms.room_id) a GROUP BY hotel_id;
编辑:
这可能做到这一点:
SELECT hotel_id,room_id,room_number,MAX(a.tc) AS "Count",MAX(tp) AS "MostProfit" FROM
(SELECT hotel_id,rooms.room_id,room_number,COUNT(rooms.room_id) AS "tc",SUM(room_price) AS "tp" FROM rooms JOIN bookings
ON rooms.room_id=bookings.room_id
GROUP BY rooms.room_id) a GROUP BY hotel_id
请尝试以下一次:
SELECT RO_BOOK.HOTEL_ID,
RO_BOOK.ROOM_ID,
RO_BOOK.ROOM_NUMBER,
RO_BOOK.TOTAL_BOOKINGS,
MAX(RO_BOOK.TOTAL_EARNINGS) PROFITS
FROM(
SELECT ROOMS.HOTEL_ID
ROOMS.ROOM_ID,
ROOMS.ROOM_NUMBER,
COUNT(ROOMS.ROOM_ID) TOTAL_BOOKINGS
SUM(ROOMS.ROOM_PRICE) TOTAL_EARNINGS
FROM
ROOMS, BOOKINGS
WHERE
BOOKINGS.ROOM_ID = ROOMS.ROOM_ID
GROUP BY ROOMS.ROOM_ID) RO_BOOK
GROUP BY RO_BOOK.HOTEL_ID ;
它类似于@ tcadidot0代码,但列MAX(a.tc)AS“计数”,不论ROOM_ID的返回的最大计数。 对于例如:如果酒店1有2间房间说,R100和R200。 R100的成本是1000和R200为100号的倍R100订为1,R200是3所以查询将返回:酒店1,R100,计数2,利润1000。
请纠正我,如果我的问题是错误的。