This one is probably a softball question for any DBA, but here’s my challenge. I have a table that looks like this:
id parent_id active
--- --------- -------
1 5 y
2 6 y
3 6 y
4 6 y
5 7 y
6 8 y
The way the system I am working on operates, it should only have one active row per parent. Thus, it’d be ok if ID #2 and #3 were active = ‘n’.
I need to run a query that finds all rows that have duplicate parent_ids who are active and flip all but the highest ID to active = ‘y’.
Can this be done in a single query, or do I have to write a script for it? (Using Postgresql, btw)
ANSI style:
Postgres specific:
The second one is probably a bit faster, since it’s not doing a correlated subquery for each row. Enjoy!