Currently, the table is ordered in ascending order by row_number
. I need help removing duplicates based on 2 conditions.
- If there is a
stage
, that isonline
then I want to keep that row, doesn't matter which one, there can be multiple. - If there isn't a row with
online
for thatorg_id
, then I keeprow_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 |