Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Nov 20, 2019 1:29 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Need help with exclusive query
PostPosted: Tue Dec 25, 2012 1:22 am 
Offline
User avatar

Joined: Thu Mar 01, 2007 2:02 pm
Posts: 237
Location: Sunny (generally!) Southern California
Well, either it's the season or old age creeping in on me again but I'm having trouble coming up with a sql query that I really should know.

Basically, I have two tables in the RealSqlDatabase. (There are actually more but only these two are involved at the moment.) One table has a complete set of data in one of its columns while the other table has a partial set in its column. What I am trying to remember is a simple sql query to retrieve the data that is in column 1 but is NOT in column 2.

Right now, I'm using a NOT IN clause mated with a sub-select but there's got to be a better way to do it. Isn't there?

Thanks.
- Dale

_________________
-----
Real Studio 2012r1 on Windows 7 (64 bit)
-----
It has been said that politics is the second oldest profession.
I have learned that it bears a striking resemblance to the first.
- Ronald Reagan


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help with exclusive query
PostPosted: Tue Dec 25, 2012 4:14 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Use NOT EXISTS with a subquery. Or use a join and select for WHERE table2.column1 IS NULL.


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help with exclusive query
PostPosted: Tue Dec 25, 2012 9:43 am 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
SELECT *
FROM tablea A
WHERE NOT EXISTS(SELECT 8
FROM table B
WHERE a.myfield=b.myfield)

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help with exclusive query
PostPosted: Tue Dec 25, 2012 3:35 pm 
Offline
User avatar

Joined: Thu Mar 01, 2007 2:02 pm
Posts: 237
Location: Sunny (generally!) Southern California
Thanks. That's basically what I'm doing except that I'm using NOT IN rather than NOT EXISTS. Is there a reason to prefer one over the other?

- Dale

_________________
-----
Real Studio 2012r1 on Windows 7 (64 bit)
-----
It has been said that politics is the second oldest profession.
I have learned that it bears a striking resemblance to the first.
- Ronald Reagan


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help with exclusive query
PostPosted: Tue Dec 25, 2012 11:40 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
NOT IN has to return a (potentially) large result and then search it for a match. NOT EXISTS just searches, so it only does half the work. Plus, it can quit early if it finds a match.

Of course, just about everything will be cached, so the real difference lies in whether the column in table B is indexed. Then it makes a difference in performance. Otherwise, it's probably a wash.


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help with exclusive query
PostPosted: Wed Dec 26, 2012 1:05 am 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
and "SELECT 8" vs "SELECT *" reduces the caching to almost nothing.. because the optimizer creates a very small return vector, where "*" would create a buffer the size required to manipulate entire records.

and why "8" everyone asks? why not "1" or "2"... it makes no difference... it just has to be "something"

but "8" is most common because it is the "unshifted" key for "*" :)

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: Need help with exclusive query
PostPosted: Wed Dec 26, 2012 3:23 pm 
Offline
User avatar

Joined: Thu Mar 01, 2007 2:02 pm
Posts: 237
Location: Sunny (generally!) Southern California
Thanks, guys. In this case, the records are fairly small and there are only a couple of thousand in each table so space and performance are not really an issue. I did change it from NOT IN to NOT EXISTS since I assume it doesn't like to do extra work any more than I do. :)

- Dale

_________________
-----
Real Studio 2012r1 on Windows 7 (64 bit)
-----
It has been said that politics is the second oldest profession.
I have learned that it bears a striking resemblance to the first.
- Ronald Reagan


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group