Hey guys I am getting this exception when trying to run the query
Column
restaurantData.restaurantIdis invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause.
Query:
SELECT
T1.restaurantId AS ID,
COUNT(T2.post_id) AS favCount
FROM
restaurantData AS T1
INNER JOIN
favoriteData AS T2 ON T1.restaurantId = T2.post_id
ORDER BY
favCount DESC, ID DESC
It says that I have to add all select columns which are not aggregated in ORDER BY clause, which I already did for restaurantID. But its still giving me error.
You are missing
GROUP BY T1.restaurantIdin the query.If you have no
GROUP BYclause at all then this gives you an aggregate of the whole source table (in which case inclusion ofrestaurantIdin the select list is invalid without wrapping that in an aggregate) rather than broken into groups by restaurant as you clearly desire.