Jump to content

Help with a join getting ONLY the latest results in the table?


Mr Chris

Recommended Posts

Hello All,

 

I have a query which looks in two tables for data and performs a join and works great:

 

SELECT     
           DISTINCT dc.order_id, 
           dc.id,
           dc.campaign_name,
           dc.campaign_start_date,
           dc.campaign_end_date,
           dc.salesperson,
           dc.ad_type,
           dws.order_id, 
           dws.impressions,
           dws.clicks
         FROM 
           cart_campaigns AS dc
         INNER JOIN 
           cart_weekly_stats AS dws ON 
           dc.order_id = dws.order_id AND dc.campaign_start_date = 
         (SELECT MAX(campaign_start_date) FROM dart_weekly_stats WHERE order_id = dc.order_id)
      WHERE dc.salesperson  = '10'
        ORDER BY 
        dc.order_id DESC

 

However, I just have one problem I simply want the result to output the LATEST row for each order_id, when as you can see in the below it outputs all the results, so I just want the highlighted latest ones returned that are both different order id's (the latest ones for that particular order_id)?

 

rows.jpg

 

Can anyone help as this has been winding me up all morning!

 

Thanks

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.