Skip to content
Advertisement

PostgreSQL: Sub-Query on one table

My table looks (simplified) like this:

It records filenames “File”; sometimes, a file (here BBB) is replaced by a newer one (here CCC”. First show all records, that have been replaced:

OK, brings up line 2. Now I need to add a subquery column QQ, which shows the File of the substitute record, e.g. should bring:

. My approach does not work:

Where is my mistake? Thank you!

Advertisement

Answer

I think you want a self-join:

This is query you wanted to write – I find that is less neat, because it needs a where clause, while the above does not:

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