I'm trying to find the average of a 'ratings' column for all the data that has the same 'VenueId'. I have it working with raw code but need to adapt it in Sequelize. The working raw code is
SELECT venueId, AVG(rating) AS average FROM reviews GROUP BY VenueId
The code I've tried is
Review.findAll({
order: [[Sequelize.fn('AVG', Sequelize.col('rating'))]]
}).then(function(Venues) {})
I'm getting an error:
Executing (default): SELECT id, review, rating, createdAt, updatedAt, VenueId FROM Reviews AS Review ORDER BY max(rating); Unhandled rejection SequelizeDatabaseError: UNKNOWN_CODE_PLEASE_REPORT: Expression #1 of ORDER BY contains aggregate function and applies to the result of a non-aggregated query.
Any ideas?