I have a table with some cities, restaurants and orders (per restaurant) for a specific year. From this dataset, I have a problem stating that:
- In cities with average orders higher than 120, restaurants with less than 100 order should be aborted.
- In cities with average orders higher than 60, restaurants with less than 40 order should be aborted.
- In cities with average orders higher than 30, restaurants with less than 20 order should be aborted.
Until now, I have created a query that can give me a table with the average orders for each city and label them as 1, 2, 3 or 4 as follows:
SELECT cities, AVG(orders), COUNT(restaurants),
CASE
WHEN (AVG(orders) >= 120) THEN '1'
WHEN (120 > AVG(orders) >= 60) THEN '2'
WHEN (60 > AVG(orders) >= 30) THEN '3'
ELSE '4'
END AS ranking
FROM c_cities
GROUP BY 1
So, now I want to create a statement that will check for example: for rating = 1, if orders > 100 -> keep them or label them as something. for rating = 2, if orders > 60 -> similar and etc.
I am trying to find the logic and the right statement in it. I think that a CASE ... WHEN cannot solve my problem. I am reading the documentation for IF statement, but I cannot make it work.
I would be grateful for any help you could provide :)
create tableandinsertstatements)