Unix Technical Forum

Help With Syntax

This is a discussion on Help With Syntax within the SQL Server forums, part of the Microsoft SQL Server category; --> I have two tables. The first table (a) has a list of all my clients. The second table (b) ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:47 PM
PeterA
 
Posts: n/a
Default Help With Syntax

I have two tables.
The first table (a) has a list of all my clients.
The second table (b) contains all of the clients who have placed
orders.
How do I get a list of all of the clients from table "a"
who have NOT placed an order based on table "b?"
Keeping in mind, that if they have not placed an order, they will not
appear in table "b."

Both tables have a client_id column

Help

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:47 PM
Jason
 
Posts: n/a
Default Re: Help With Syntax

Try one of these:

SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
tableB)

SELECT a.*
FROM tableA a
LEFT JOIN tableB b ON a.client_id = b.client_id
WHERE b.client_id IS NULL

you should see better performance on the second one if you have a lot
of rows in your tables.

Jason

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:47 PM
SQL Menace
 
Posts: n/a
Default Re: Help With Syntax

be carefull with NULLS and IN, if tableB has even 1 NULL for the
client_id column nothing will be returned
better to add IS NOT NULL

SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
tableB WHERE client_id IS NOT NULL )

or use NOT EXISTS

SELECT * FROM tableA A WHERE NOT EXISTS (SELECT * FROM
tableB WHERE client_id = A.client_id )


Denis the SQL Menace
http://sqlservercode.blogspot.com/


Jason wrote:
> Try one of these:
>
> SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
> tableB)
>
> SELECT a.*
> FROM tableA a
> LEFT JOIN tableB b ON a.client_id = b.client_id
> WHERE b.client_id IS NULL
>
> you should see better performance on the second one if you have a lot
> of rows in your tables.
>
> Jason


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:47 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Help With Syntax

SQL Menace (denis.gobo@gmail.com) writes:
> or use NOT EXISTS
>
> SELECT * FROM tableA A WHERE NOT EXISTS (SELECT * FROM
> tableB WHERE client_id = A.client_id )


Yes, this is the preferred syntax for the given problem, as it clearly
expresses what it's all about.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 07:47 PM
PeterA
 
Posts: n/a
Default Re: Help With Syntax

Thanks, Jason. I got it to work using basically this core of code. I
had simplified the question for the purpose of getting a straight
answer, but your code is the core of the soluton. Much appreciated!!


Jason wrote:
> Try one of these:
>
> SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
> tableB)
>
> SELECT a.*
> FROM tableA a
> LEFT JOIN tableB b ON a.client_id = b.client_id
> WHERE b.client_id IS NULL
>
> you should see better performance on the second one if you have a lot
> of rows in your tables.
>
> Jason


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 07:47 PM
--CELKO--
 
Posts: n/a
Default Re: Help With Syntax

>> The first table (a) has a list of all my clients. The second table (b) contains all of the clients who have placed orders. <<

The quick answer is to use a LEFT OUTER JOIN or an EXISTS() predicate.


The right answer is to ask why you consider these clients to be
logically diffferent entities. It sounds like what you need is one
table with a status code of some kind to tell you when you have a
client who placed an order versus one who has not (and who returns
orders, etc.).

Why persist redundant data in physical storage? This is called
"attribute splitting" -- you are converting an attribute into a table.

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 07:46 AM.


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