I am trying to find a query that will tell me the values that are not in my database. Eg:
select seqID, segment from flu where seqID IN (1,2,3,4,5,6,7,8,9).
Now if my database doesn't have seqID's 3,8,9 how would I find/display only the missing seqID's.
We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.
PostgreSQL provides you with two wildcards: Percent sign ( % ) matches any sequence of zero or more characters. Underscore sign ( _ ) matches any single character.
The information schema is a built-in schema that's common to every PostgreSQL database. You can run SQL queries against tables in the information_schema to fetch schema metadata for a database.
<> is the standard SQL operator meaning "not equal". Many databases, including postgresql, supports != as a synonym for <> . They're exactly the same in postgresql.
First, since you appear to be new to Stackoverflow, here's a few etiquette and posting tips:
Since you didn't do all of the above, I'm left guessing, so I'm making some assumptions based on your code. You seem to need an EXCEPT statement. The following code was developed on PostgreSQL 9.1.
create temp table my_value(seq_id int);
insert into my_value(seq_id) values
(1), (2), (4), (5), (6), (7);
select unnest(array[1, 2, 3, 4, 5, 6, 7, 8, 9])
EXCEPT
select distinct seq_id from my_value;
I'm assuming that you are have a hard coded list of ints (like in your example in the question). I just created a temp table for testing and demo purposes, but I'm sure you can make the necessary adjustments to work in your situation. If you don't have a hard-coded list of ints, then you just need to do a select again whatever source would contain it.
Hope this helps. Welcome to Stackoverflow.
with idlist (id) as (
values (1),(2),(3),(4),(5),(6),(7),(8),(9)
)
select l.id as missing_seq_id
from idlist l
left join flu f on f.seqID = l.id
where f.seqID is null;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With