Unix Technical Forum

select ..... not in .....

This is a discussion on select ..... not in ..... within the pgsql Sql forums, part of the PostgreSQL category; --> 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 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 03:24 PM
Gary Stainburn
 
Posts: n/a
Default select ..... not in .....

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

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 03:24 PM
Tom Lane
 
Posts: n/a
Default Re: select ..... not in .....

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

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:39 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com