Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old August 17th, 2005, 11:55 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
__________________
Clay Hess
  #2 (permalink)  
Old August 17th, 2005, 12:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
  #3 (permalink)  
Old August 17th, 2005, 12:58 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Clay Hess


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excluding certain records from a search RussLewis XSLT 5 March 4th, 2008 12:14 PM
Including some records and excluding others RussLewis XSLT 2 March 3rd, 2008 12:36 PM
excluding archives of a folder thomaz C# 4 March 8th, 2004 09:09 AM
excluding html from printer .. qwjunk Classic ASP Basics 1 February 3rd, 2004 04:09 PM
Excluding items with certain keywords ashcarrot SQL Language 4 July 23rd, 2003 10:04 AM





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