Skip to content
Advertisement

Filtering Duplicates after UNION

I have 2 tables: Activities & Edited they are pretty the same but Edited contains a few additional columns that allow me to process data offline. The record may be at one of the tables or in both.

I need to take a bunch of data that will contain only a unique activityKey. The latest result should be based on editDate. For example:

enter image description here

But It only collects the data from both tables. And I get duplications like in the screenshot: WO-981 & WO-924. Question: How I can remove these duplicates and use only the newest records based on editDate. Also, I need to remove the record if deleted = 1 at editDate and deleted = 0 at activities table as with WO-924 it should be removed.

Advertisement

Answer

You can use aggregation for this, if deleted takes on only the values of 0 and 1:

The more general solution is to use ROW_NUMBER(), but this works for this case.

EDIT:

If you want to skip rows with deleted = 1, then add the HAVING clause:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement