I have two tables:
contacts:
id, name
1 Alex
2 John
documents:
id, contactID, type
1 1 1
2 1 2
...
30 1 3
31 2 1
32 2 3
I want to get contact name, and document type. the only record for each contact. Contact may have several document types (the only document per type), and I have the following priority for document types: 2, 3, 1.
For example, Alex has documents of all types, but I should get only:
Alex, 2
John has document types = 1, 3, as a result I should get:
John, 3
Oracle database. But if you’ll be able to give sql standard solution, it would be great
Oracle 9i+, use:
…or the non-Subquery Factoring (AKA CTE) version (still 9i+):