title: Filtering Duplicates after UNION
body: <p>I have 2 tables: <code>Activities</code> &amp; <code>Edited</code> they are pretty the same but <code>Edited</code> contains a few additional columns that allow me to process data offline. The record may be at one of the tables or in both.</p>
  
  <p>I need to take a bunch of data that will contain only a unique <code>activityKey</code>. The latest result should be based on <code>editDate</code>. For example:</p>
  
  <pre><code>SELECT activityKey, editDate, deleted
  FROM activities
  WHERE projectID = '4'  
  AND deleted = 0
  UNION
  SELECT activityKey, editDate, deleted
  FROM edited
  WHERE projectID = '4' 
  ORDER BY editDate DESC
  LIMIT 20 OFFSET 0
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/A6Koi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A6Koi.png" alt="enter image description here"></a></p>
  
  <p>But It only collects the data from both tables. And I get duplications like in the screenshot: <code>WO-981</code> &amp; <code>WO-924</code>.
  Question:
  How I can remove these duplicates and use only the newest records based on <code>editDate</code>.
  Also, I need to remove the record if <code>deleted = 1</code> at <code>editDate</code> and <code>deleted = 0</code> at <code>activities</code> table as with <code>WO-924</code> it should be removed.</p>