Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 11th, 2003, 02:01 PM
Registered User
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Subselect?

I have two tables, say tableA and tableB. TableA has 515 records and 513 of them were automatically dumped into tableB. I'm trying to find out which records from tableA did not make it into tableB. Here's part of my sql below..

SELECT Test.checknumber from Test, Approved_Details_Table WHERE Test.checknumber = Approved_Details_Table.Check_Number

This will get me the 513 records but I don't want to sort through the 515 to see what the 2 are... do I create a subselect to find this out?

Thanks.

 
Old December 11th, 2003, 02:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

SELECT * FROM Test A WHERE NOT EXISTS(SELECT * FROM Approved_Details_Table B WHERE A.Check_Number = B.Check_Number)

 
Old December 11th, 2003, 02:21 PM
Registered User
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perfect... Thanks

 
Old December 11th, 2003, 02:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Another way to do this is via an OUTER JOIN:
Code:
SELECT TableA.*
    FROM TableA LEFT JOIN TableB ON TableA.CheckNumber=TableB.CheckNumber
WHERE TableB.CheckNumber IS NULL;
This works because the OUTER JOIN preserves all the rows of TableA, and sets the corresponding values of TableB to NULL if there is no match based on the CheckNumber. The WHERE clause then selects only those (missing from TableB) rows.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help - Nested/Subselect SQL taylor99ss Oracle 4 November 1st, 2006 05:03 AM
Simple subselect & image link ymoisan Excel VBA 0 May 4th, 2004 10:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.