mysql - Select only unique rows with INNER JOIN query -


this question has answer here:

sorry, not quite sure how word this.

there 2 database tables, store_locations , store_diagnostics.

store_locations holds static information store, such zip_code, storeid , few other things. while store_diagnostics holds diagnostic data , stats on each store location, such total_sold, total_revenue, etc.

new entries entered store_diagnostics every hour or via cron job checks total sales, etc. issue on behalf instead of updating existing rows, new row created each time cron job runs sales week can compared last week etc.

my current query works, fetches multiple entries same store. ideally, i'd fetch latest store_diagnostics row given storeid , order them created field descending last created row particular store.

here existing query:

select   locations.* ,   location_stats.total_sold ,   location_stats.total_moved_to ,   location_stats.total_moved_from ,   location_stats.total_revenue ,   location_stats.created store_locations locations   inner join     store_diagnostics location_stats   on     locations.storeid = location_stats.storeid order location_stats.created desc , location_stats.total_sold desc 

as can see, i'd fetch 1 result per storeid , i'd fetched after ordering rows created column in desc order.

the location_stats.total_sold sorting method i'd used on results, unique stores sorted total amount of items sold.

i sorry if unclear, hope have explained sufficiently.

i added clause filters based on maximum created top value if sorted in descending order. overall sort order changed total_sold indicated in problem description. note if cron job generates new rows faster precision of created, you'll duplicates. i'm assuming doesn't. if have identity pk in store_diagnostics, i'd take max of rather created.

option 1: use correlated subquery

select   locations.* ,   location_stats.total_sold ,   location_stats.total_moved_to ,   location_stats.total_moved_from ,   location_stats.total_revenue ,   location_stats.created store_locations locations   inner join store_diagnostics location_stats on locations.storeid = location_stats.storeid location_stats.created = (select max(created) store_diagnostics stored_id = locations.stored_id) order location_stats.total_sold desc 

option 2: join groupby

select   locations.* ,   location_stats.total_sold ,   location_stats.total_moved_to ,   location_stats.total_moved_from ,   location_stats.total_revenue ,   location_stats.created store_locations locations   inner join store_diagnostics location_stats on locations.storeid = location_stats.storeid   inner join (select store_id, max(created) created stored_diagnostics group store_id) mx on locations.store_id = mx.stored_id , location_stats.created = mx.created order location_stats.total_sold desc 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -