I have a query joining multiple tables and returning multiple columns.
The indexed column of another table refers to the PK of one of these joined tables. Now I would like to add another column to the query that indicates whether at least one row with this identifier exists in the new table.
So, if I have one of the old tables
ID 1 2 3
and new table
REF_ID 1 1 1 3
then i would like to get
ID REF_EXISTS 1 1 2 0 3 1
I can come up with several ways to do this, but what is the most elegant / efficient?
EDIT I tested the performance of queries containing 50,000 records in the old table, each other record being matched by two rows in the new table, so half of the records have REF_EXISTS = 1.
I add average results as comments to the answers in case someone is interested. Thanks everyone!
source share