Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

How to get a query of all customers that have not been shipped a product

From a recent email:

Steve,
 
Need to run a query in SQL to find customers who have not purchased a particular product.  In this case “FNADE*”
 
I can easily query customers who have purchased a particular or range of products, but never had to query a list of customers who have not purchased a particular product…
 
Can you get me going in the right direction?
 
Thanks.

Answer:

This is a tricky one. There are several ways to do it, but this seems simplest:

First, get a list of customers that HAVE gotten a product

      select distinct custnmbr
            from sop30200 h
                  join sop30300 l on l.sopnumbe = h.sopnumbe and h.soptype = l.soptype
            where itemnmbr like 'SERVICE, HOURLY'

Then, get everyone else (note the query above is used in the where clause below. )
select custnmbr
      from rm00101 r
      where custnmbr not in (
      select distinct custnmbr
            from sop30200 h
                  join sop30300 l on l.sopnumbe = h.sopnumbe and h.soptype = l.soptype
            where itemnmbr like 'SERVICE, HOURLY'
      )


Comments

No Comments

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.