Given a postgres table containing the following items:
tag_id, duplicate_tag_id, tag_created_at_timestamp
14175, 14178, ...
14175, 14177, ...
14176, null, ...
14177, 14178, ...
14178, 14179, ...
14179, null, ...
14180, null, ...
14181, null, ...
Is it possible to write a query that will return tag_id
s that don’t have an entry in duplicate_tag_id
column Unless that entry is corresponds to a tag_id that exists in the duplicate_tag_id in a previous row
So, in the case above, I would like the result to be:
14175
14176
14179
14180
14181
14179 is included because it was the duplicate_tag for 14178, which was already a duplicate_tag of 14175
Thanks!
2
Answers
Just select the tag_id’s not duplicated but keeping the ones that are equal to its previous duplicate_tag_id, but not appear more than once in duplicate_tag_id, like:
Result
Fiddle to test
I am not sure if I understood completely the question, the following query return all
tag_id
ifduplicate_tag_id
is null (it does not exist) or ifduplicate_tag_id
equal totag_id
of the previous row .Query
See example