Jump to content

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


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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.