Ok so I have a ratings table. A new row is created for each user that leaves a rating. What I am atempting to do is create a query that will return the rating value for each specific row that exists now and 7 days ago.
The broad view is that I want to calculate the difference in the total average rating vs the total average rating of 7 days ago.
I have this which I think is the correct road to go down but I’m getting
Unknown column 'start_ratings.menu_item_id' in 'on clause'
SELECT *
FROM ratings end_ratings
JOIN (
SELECT menu_item_id as end_menu_item_id, AVG(value) AS end_rating
FROM ratings
WHERE
created_at > current_date - 7
AND menu_item_id IS NOT NULL
AND location_id IS NOT NULL
GROUP BY menu_item_id
) start_ratings ON (end_ratings.menu_item_id = start_ratings.menu_item_id)
WHERE menu_item_id IS NOT NULL
AND location_id IS NOT NULL;
You have aliased the column
as end_menu_item_id. Just use that instead, as only the alias is known outside of the derived table’s()in theONclause,SELECTlist, etc…Because of this:
Use this: