p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   SQL Language (http://p2p.wrox.com/forumdisplay.php?f=100)
-   -   Excluding Rows (http://p2p.wrox.com/showthread.php?t=32588)

fs22 August 17th, 2005 11:55 AM

Excluding Rows
 
I have an orders table that contains customer product orders. It has a model number field and a customer number field. Here is an example of the table...

[u]Customer Number Model Number</u>
123 aaa
123 bbb
124 aaa
124 bbb
125 aaa
125 bbb
125 ccc

What I would like to do is get a query that excludes customers that have ordered a specific model number. FOr example, if I want to see customers that have ordered products excepp for model number 'ccc', I want it to pull the following info...

[u]Customer Number</u>
123
124

In my SQL statement, it is pulling the following...

[u]Customer Number</u>
123
124
125

What it is doing is excluding the model number 'ccc', but still includes the customer 125 because they have ordered other products. Does any have any thoughts on how to do this correctly?

Note that the above are simply examples of the tables. If you would like to see my actual SQL statement, here it is...

SELECT DISTINCT CUSTOMERNUMBER
FROM tbl_orders
WHERE ProductModelNumber <> 'H5160R'

Thanks in advance

Clay Hess

Jeff Mason August 17th, 2005 12:47 PM

A subquery should do the trick.

It's sometimes helpful to think of these from the "inside out":

First find the set of CUSTOMERNUMBER's which have ordered the ProductModelNumber you want to exclude:
Code:

SELECT CUSTOMERNUMBER
  FROM tbl_orders
WHERE ProductModelNumber = 'H5160R'

Then the result you are looking for are those customers NOT in this set:
Code:

SELECT DISTINCT CUSTOMERNUMBER
FROM tbl_orders
WHERE CUSTOMERNUMBER NOT IN
    (SELECT CUSTOMERNUMBER
      FROM tbl_orders
    WHERE ProductModelNumber = 'H5160R')


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

fs22 August 17th, 2005 12:58 PM

That did the trick. I was thinking at it from the outside in. Thanks for the assist.

Clay Hess


All times are GMT -4. The time now is 07:34 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.