I want to select elements that are not present in the database, but exist in a postgres array. To visualize:
Assume that I have this data in my table:
I have come close after some research, but I do not get exactly what I expected. My current query is:
WITH res AS (SELECT entity_number FROM entity_coordinates WHERE entity_number IN (
'MG1735401016/6',
'NON-EXIST-1',
'P171025002876-1',
'P170321400780-1',
'NON-EXIST-2'
))
SELECT *
FROM unnest(ARRAY[
'MG1735401016/6',
'NON-EXIST-1',
'P171025002876-1',
'P170321400780-1',
'NON-EXIST-2'
]) item_id
FULL OUTER JOIN res ON entity_number=item_id
My expected result is:
I tried different join types but no luck.
Any help is appreciated
2
Answers
After some research I found another solution that uses
EXCEPT
:But my benchmarks show that @nafrolov's solution is nearly twice as fast, so I accepted his answer
So, this query should achieve what you’re trying to do without any
JOIN
s (NOT EXISTS
is the most straight forward way to check if any entity_number corrsponds withstr
(element of array)):