Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #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
Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:38 PM.


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