Here are my three relevant sql tables with their relevant properties:
- OrderType with property
ID - RestaurantVisit, with property
OrderTypeIDandRestaurantID - Restaurant, with property
ID
When given an Order Type (eg Drink, Side, Entree), I need to return all of the restaurants that have ordered that order type before, ordered by which restaurant has the most orders of that type.
For example, given type side, I return Restaurants r1, r2, r3, where r1 is the restaurant that has the most orders of side out of all the restaurants, r2 is the one with second most, etc.
This is what I have so far (remember, I’m given an OrderTypeID already):
SELECT DISTINCT r.ID
FROM Restaurant r
JOIN RestaurantVisit rv
ON rv.OrderTypeID = ?
WHERE rv.ID = r.ID
This currently works to return all of the restaurants that have ever received an order of the given type. What I need to do now is order these by the restaurants that have the most of orders of the given type.
What I’m thinking is to add something like this somewhere, but I’m not at all sure:
ORDER BY COUNT(rv.OrderTypeID = ?)
Anyone have any ideas? Thanks!
The ANSI/ISO standard for ORDER BY is to use the
column alias. The (INNER) JOINs will ensure the type has been ordered by the Restaurant before.