Help with some SQL

Hi Experts,

I am trying to come up with a Query that will give me which customers have purchased Product 1, but not Product 2.

My Tables and fields are:

Customers
   CustomerID
   etc.

Purchases
   CustomerID
   ProductName   e.g. Product 1, Product 2 etc.
   

Notes:
   Customers may have purchased same product on different dates

Regards,
Leigh
LVL 1
LeighWardleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
Try this:

SELECT [Customers].* FROM [Customers]
    INNER JOIN [Purchases] ON [Purchases].CustomerID = [Customers].CustomerID
WHERE [Purchases].ProductName = 'Product 1'
    AND [Customers].CustomerID NOT IN (
        SELECT CustomerID FROM [Purchases] WHERE ProductName = 'Product 2'
    )

Open in new window

0
gurpsbassiCommented:
How about this:
select distinct c.CUSTOMERID, c.NAME FROM customers c inner join PURCHASES p on (c.CUSTOMERID = p.CUSTOMERID)
where p.PRODUCTNAME = 'Product 1' and not exists (select 1 from PURCHASES p1 where p1.productname = 'Product 2' and p1.customerid = c.CUSTOMERID) 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LeighWardleAuthor Commented:
Thanks, Duy Pham and gurpsbassi, both solutions work with my test data.

Regards,
Leigh
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

LeighWardleAuthor Commented:
When I ran my first test, I did not have more than one purchase of a given Product for a given Customer.

Here is my new test data - this  time I have more than one purchase of a given Product for a given Customer.

Customer
CustomerID      Name
1                      Customer has Product 1 and Product 2
2                      Customer has Product 1, but not Product 2
4                      Customer has Product 2, but not Product 1

Purchases
ID      CustomerID      ProductName
1      1      Product 1
2      1      Product 2
3      2      Product 1
4      4      Product 2
5      1      Product 1
6      1      Product 2
7      2      Product 1
8      4      Product 2

With this data, Duy Pham's query returns the correct customer, but has 2 instances of it:

CustomerID      Name
2                      Customer has Product 1, but not Product 2
2                      Customer has Product 1, but not Product 2

gurpsbassi's query gives the correct answer:

CustomerID      Name
2                      Customer has Product 1, but not Product 2
0
gurpsbassiCommented:
It's the use of distinct.
0
LeighWardleAuthor Commented:
Yes gurpsbassi, adding Distinct to Duy Pham's query fixed it.

Regards,
Leigh
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.