Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select won't work when using IN()

Author  Topic 

soulchyld21
Starting Member

28 Posts

Posted - 2012-02-16 : 04:37:57
This code works perfectly,
SELECT distinct Customer_Number, SUM(Price) As Total
FROM Customer_Offer x1
WHERE
EXISTS
(SELECT * FROM Customer_Offer x2 WHERE x2.Customer_Number = x1.Customer_Number
AND x2.Product_Offer = 'prod1')
AND NOT EXISTS
(SELECT * FROM Customer_Offer x3 WHERE x3.Customer_Number = x1.Customer_Number AND
x3.Product_Offer IN ( 'prod2','prod3', 'prod4') )
GROUP BY Customer_Number
ORDER BY Customer_Number;


however I would like to find customers who have bought prod1 and prod2 and not bought prod3 and prod4, I tried using this code
SELECT distinct Customer_Number, SUM(price) As Total
FROM Customer_Offer x1
WHERE
EXISTS
(SELECT * FROM Customer_Offer x2 WHERE x2.customer_number = x1.customer_number
AND x2.product_offer IN ('prod1', prod2)
AND NOT EXISTS
(SELECT * FROM Customer_Offer x3 WHERE x3.customer_number = x1.customer_number AND
x3.product_offer IN ( 'prod3','prod4') )
GROUP BY Customer_number
ORDER BY Customer_Number;

but get a syntax error! is anyone able to point me in the right direction for what I am trying to achieve?

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-02-16 : 09:26:48
quote:
Originally posted by soulchyld21

This code works perfectly,
SELECT distinct Customer_Number, SUM(Price) As Total
FROM Customer_Offer x1
WHERE
EXISTS
(SELECT * FROM Customer_Offer x2 WHERE x2.Customer_Number = x1.Customer_Number
AND x2.Product_Offer = 'prod1')
AND NOT EXISTS
(SELECT * FROM Customer_Offer x3 WHERE x3.Customer_Number = x1.Customer_Number AND
x3.Product_Offer IN ( 'prod2','prod3', 'prod4') )
GROUP BY Customer_Number
ORDER BY Customer_Number;


however I would like to find customers who have bought prod1 and prod2 and not bought prod3 and prod4, I tried using this code
SELECT distinct Customer_Number, SUM(price) As Total
FROM Customer_Offer x1
WHERE
EXISTS
(SELECT * FROM Customer_Offer x2 WHERE x2.customer_number = x1.customer_number
AND x2.product_offer IN ('prod1','prod2') -- missed the single quotes
AND NOT EXISTS
(SELECT * FROM Customer_Offer x3 WHERE x3.customer_number = x1.customer_number AND
x3.product_offer IN ( 'prod3','prod4') )
GROUP BY Customer_number
ORDER BY Customer_Number;

but get a syntax error! is anyone able to point me in the right direction for what I am trying to achieve?



http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-16 : 09:37:47
I believe that's an OR Condition


EXISTS (SELECT * FROM Customer_Offer p1
WHERE x1.customer_number = p1.customer_number
AND p1.product_offer = 'prod1')
AND EXISTS (SELECT * FROM Customer_Offer p2
WHERE x1.customer_number = p2.customer_number
AND p2.product_offer = 'prod2')
AND NOT EXISTS (SELECT * FROM Customer_Offer p3
WHERE x1.customer_number = p3.customer_number
AND p3.product_offer IN ('prod3','prod4'))








Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sathiesh2005
Yak Posting Veteran

85 Posts

Posted - 2012-02-16 : 10:16:19
try this. Note changes
SELECT distinct Customer_Number, SUM(price) As Total
FROM Customer_Offer x1
WHERE
EXISTS
(SELECT * FROM Customer_Offer x2 WHERE x2.customer_number = x1.customer_number
AND x2.product_offer IN ('prod1',' prod2'))
AND NOT EXISTS
(SELECT * FROM Customer_Offer x3 WHERE x3.customer_number = x1.customer_number AND
x3.product_offer IN ( 'prod3','prod4') )
GROUP BY Customer_number
ORDER BY Customer_Number;



@Sathiesh
Go to Top of Page
   

- Advertisement -