pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
select ..... not in .....
2+ messages / 2 participants
[nested] [flat]

* select ..... not in .....
@ 2007-05-14 15:07 Gary Stainburn <gary.stainburn@ringways.co.uk>
  2007-05-14 15:37 ` Re: select ..... not in ..... Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Gary Stainburn @ 2007-05-14 15:07 UTC (permalink / raw)
  To: pgsql-sql

I folks.

I'm struggling with a select which should be easy.

 select v_d_code, v_o_number,  v_vin, v_status from vista_details where 
v_d_code='64340' and v_o_number='C0023';
 v_d_code | v_o_number |    v_vin    |  v_status
----------+------------+-------------+-------------
 64340    | C0023      | GCDW7D37645 | COMPOUND IN
(1 row)

shows that the record exists in vista.

select w_vin from walon where substring(w_vin from '(.{11}$)') 
= 'GCDW7D37645';
 w_vin
-------
(0 rows)

shows that it doesn't exist in walon.

So, who does this select not return the row?

select v_d_code, v_o_number,  v_vin, v_status from vista_details where v_vin 
not in (
goole(#   select substring(w_vin from '(.{11}$)') from walon);
 v_d_code | v_o_number | v_vin | v_status
----------+------------+-------+----------
(0 rows)

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000     



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: select ..... not in .....
  2007-05-14 15:07 select ..... not in ..... Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2007-05-14 15:37 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2007-05-14 15:37 UTC (permalink / raw)
  To: Gary Stainburn <gary.stainburn@ringways.co.uk>; +Cc: pgsql-sql

Gary Stainburn <gary.stainburn@ringways.co.uk> writes:
> So, who does this select not return the row?

> select v_d_code, v_o_number,  v_vin, v_status from vista_details where v_vin 
> not in (
> goole(#   select substring(w_vin from '(.{11}$)') from walon);

NOT IN with a sub-select that returns any NULL values cannot succeed;
the result is either FALSE (definite match) or NULL (because of the
NULL comparison results).

You could work around that with a COALESCE, but I think a less klugy
and better-performing answer would be to write it as a left join:

select v_d_code, v_o_number,  v_vin, v_status from vista_details
left join walon on (v_vin = substring(w_vin from '(.{11}$)'))
where walon.some-never-null-column IS NULL;

The where-clause rejects any actual join matches...

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-05-14 15:37 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-05-14 15:07 select ..... not in ..... Gary Stainburn <gary.stainburn@ringways.co.uk>
2007-05-14 15:37 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox