Forum Moderators: open
Table A: items
iid (primary key)
store_id
sale_avg (integer)
Table B: store
store_id (primary key)
store_name
Table C: store_location
locit (primary key)
store_id
address
city
state
zip
1 store can have many locations in Table C (store_location) and more than 1 item in table A (items)
What I am trying to accomplish is to create a sql query that would join the 3 tables to present top 10 cities with most sales.
sale_avg field in table A has this value, the challenge is to somehow join it with table C (store_location) and get the average for top 10 cities.
Thank you.
SELECT city, state, avg( sale_avg ) AS save FROM store_location
LEFT JOIN item ON (item.store_id = store_location.store_id )
GROUP BY city
ORDER BY save DESC
SELECT city, state, avg( sale_avg ) AS save
FROM store_location INNER JOIN item
ON (item.store_id = store_location.store_id )
GROUP BY city
ORDER BY save DESC
SELECT city, state, avg( sale_avg ) AS save
FROM store_location INNER JOIN item
ON (item.store_id = store_location.store_id )
GROUP BY city, state
ORDER BY save DESC
This shouldn't cause any temp tables and is the most efficent solution. Having an index on city,state will probably speed the query up.