Home > Net >  Remove duplicates based on condition and keep oldest element
Remove duplicates based on condition and keep oldest element

Time:08-25

Currently, the table is ordered in ascending order by row_number. I need help removing duplicates based on 2 conditions.

  1. If there is a stage, that is online then I want to keep that row, doesn't matter which one, there can be multiple.
  2. If there isn't a row with online for that org_id, then I keep row_number = 1 which would be the oldest element.
sales_id org_id stage row_number
ccc_123 ccc off-line 1
ccc_123 ccc off-line 2
ccc_123 ccc online 3
abc_123 abc off-line 1
abc_123 abc power-off 2
zzz_123 zzz power-off 1

so the table should look like this after:

sales_id org_id stage
ccc_123 ccc online
abc_123 abc off-line
zzz_123 zzz power-off

tabular results

  • Related