0

SQL Error (1055): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'a.room id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

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

trying to get the rooms of hotels that got the most profit in a query

3
  • You really need to explain what you want to do. It might surprise you, but a non-functioning query does not necessary express what you want to do. Commented Apr 6, 2019 at 22:52
  • its query Get room which made the most profits for each hotel
    – Abood 4433
    Commented Apr 6, 2019 at 22:55
  • stackoverflow.com/questions/41887460/… the answer is useless and does not work
    – Abood 4433
    Commented Apr 6, 2019 at 23:06

1 Answer 1

0

The error message implies that you need to convert your group by clause to

GROUP BY hotel_id, room_id, room_number

since the other columns tc and tp are aggregated, and no need to be included in the group by list, whereas room_id and room_number are not.

Not the answer you're looking for? Browse other questions tagged or ask your own question.