1

If I have an index table

Index
ID  Last    First
01  Smith   Frank
02  Biden   Joe
03  Trump   Don

and another table showing those who meet a certain measure

Measure
ID  Last    First
02  Biden   Joe
04  Obama   Barack

how can I query this in postgresql to show a listing of those on the index table and their status of meeting the measure?

Desired Output
ID  Last    First  Measure
01  Smith   Frank  No
02  Biden   Joe    Yes
03  Trump   Don    No

1 Answer 1

1

You could use a left anti-join approach:

SELECT
    i.ID,
    i.Last,
    i.First,
    CASE WHEN m.ID IS NOT NULL THEN 'Yes' ELSE 'No' END AS Measure
FROM "Index" i
LEFT JOIN Measure m
    ON m.Last = i.Last AND m.First = i.First
ORDER BY i.ID;

Note that you should, in general, avoid naming your tables and other database objects using reserved SQL keywords like INDEX. I have placed your INDEX table into double quotes above for this reason.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.