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.
| Author |
Topic |
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-08 : 10:52:03
|
Hi, I have come across an unusual error, The code below does not return any values, but if I select top 1000 rows from the Customer_Officer table I can see that both offers do exist and are indeed in the table, however they are in different rows (this should not make a difference? should it!)! Could it be a design flaw? I need a query that will return the customer number for a customer that has bought two different offers! Running the above query with only one condition, (Where) returns the correct results under either condition but not under both!SELECT Customer_Number FROM Customer_OfferWHERE Product_Offer = 'Offer2'AND Product_Offer = 'Offer1' |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
mhorseman
Starting Member
44 Posts |
Posted - 2012-02-08 : 11:44:45
|
| I think you're looking for Customers who have bought two offers--Set up datadrop table #xcreate table #x(customer_number int,product_offer varchar(10))insert into #xselect 1,'Blah' unionselect 1,'Offer1' unionselect 2,'Offer1' unionselect 3,'Offer1' unionselect 3,'Offer2' unionselect 4,'Offer2'--Execute Queryselect customer_number from #x--where product_offer in ('Offer1','Offer2')group by customer_numberhaving count(distinct product_offer) = 2If you're specifically interested only in those who've bought "Offer1" and "Offer2", then uncomment the where clause,otherwise you'll get Customers who've bought any two productsMark |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-08 : 17:06:09
|
| Thanks, I also have to write a similar query that will return customers who have bought product_one and NOT bought product_2 or product_3, How would I construct that part of the query,Many thanks |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-02-08 : 19:05:21
|
Using Mark's test date and Brett's thoughts, you can construct the query sort of like this:create table #x(customer_number int,product_offer varchar(10))insert into #xselect 1,'Blah' unionselect 1,'Offer1' unionselect 2,'Offer1' unionselect 3,'Offer1' unionselect 3,'Offer2' unionselect 4,'Offer2'SELECT distinct customer_numberFROM #x x1WHERE EXISTS (SELECT * FROM #x x2 WHERE x2.customer_number = x1.customer_number AND x2.product_offer = 'Offer1') AND NOT EXISTS (SELECT * FROM #x x3 WHERE x3.customer_number = x1.customer_number AND x3.product_offer IN ( 'Offer2','Offer3') );drop table #x |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 05:51:21
|
Thanks Brett! Lol, My final year project is slightly more complicated,Oracle and Application Express! arghhhhhhhhIm getting this application coming along, its for someone to be able to analyse their customers! Can I pick your brain a little more? We need to sum up their totals, I tried putting the sum function in my select query like thisSELECT Customer_Number, SUM(total) AS Total_Value FROM Customer_OfferWHERE Product_Offer IN ('prod1', 'prod2')AND Product_Offer NOT IN ('prod3','prod4')GROUP BY Customer_NumberThis works fine, however I need their totals for other products! say prod5, prod6 etc, (other products they may have bought), but my query only returns for prod1 and 2, |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 06:46:37
|
| For this project, SQL server 2008, for my uni work 11GI Have removed the IN() part of the where clause in the above query and got the expected results however I am afraid I could end up with customers that have not bought product one and two (inevitably), (which I need to know) is there a way I can ensure those products have been purchased but not limit the rows for the sum() function? |
 |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 06:48:31
|
| lol, assuming you were referring to software version above! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-02-09 : 07:04:15
|
How about...CREATE TABLE #Customer_Offer (Customer_Number int, Product_Offer varchar(10), Total money)GOINSERT INTO #Customer_Offer (Customer_Number, Product_Offer, Total)SELECT 1,'Blah', 10.00 UNION ALLSELECT 1,'Offer1', 20.00 UNION ALL -- 1 Won't Show up, Needs 1, 2, 5, Blah means nothingSELECT 2,'Offer1', 30.00 UNION ALL -- 2 Only Has 1SELECT 3,'Offer1', 40.00 UNION ALL -- 3 Will Show Up HAS 1,2,5 and No 3 and 4SELECT 3,'Offer2', 50.00 UNION ALLSELECT 3,'Offer5', 55.00 UNION ALLSELECT 4,'Offer1', 60.00 UNION ALL -- 4 won't show up, has 1,2,5 AND a 4SELECT 4,'Offer2', 40.00 UNION ALLSELECT 4,'Offer5', 50.00 UNION ALLSELECT 4,'Offer4', 60.00 UNION ALLSELECT 5,'Offer1', 70.00 UNION ALL -- 5 won't show up, has 1,2,5 AND 3 & 4SELECT 5,'Offer2', 80.00 UNION ALLSELECT 5,'Offer5', 90.00 UNION ALLSELECT 5,'Offer3', 90.99 UNION ALLSELECT 5,'Offer4', 99.99GO SELECT CO.Customer_Number , SUM(Prod_Offer1_Total) AS SUM_Prod_Offer1_Total , SUM(Prod_Offer2_Total) AS SUM_Prod_Offer2_Total , SUM(Prod_Offer5_Total) AS SUM_Prod_Offer5_Total FROM #Customer_Offer CO INNER JOIN (SELECT Customer_Number, Product_Offer, Total AS Prod_Offer1_Total FROM #Customer_Offer WHERE Product_Offer = 'Offer1') AS O1 ON CO.Customer_Number = O1.Customer_NumberINNER JOIN (SELECT Customer_Number, Product_Offer, Total AS Prod_Offer2_Total FROM #Customer_Offer WHERE Product_Offer = 'Offer2') AS O2 ON CO.Customer_Number = O1.Customer_NumberINNER JOIN (SELECT Customer_Number, Product_Offer, Total AS Prod_Offer5_Total FROM #Customer_Offer WHERE Product_Offer = 'Offer5') AS O5 ON CO.Customer_Number = O5.Customer_Number WHERE NOT EXISTS (SELECT * FROM #Customer_Offer N4 WHERE Product_Offer = 'Offer4' AND N4.Customer_Number = CO.Customer_Number) OR NOT EXISTS (SELECT * FROM #Customer_Offer N5 WHERE Product_Offer = 'Offer5' AND N5.Customer_Number = CO.Customer_Number)GROUP BY CO.Customer_NumberGODROP TABLE #Customer_OfferGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 07:24:34
|
| It was union I needed to use :-D, Thanks a million Brett,Here's the query I wrote and it works, just testing it now. thanks again for all your timeSELECT Customer_Number, SUM(Price) As Total FROM Customer_Offer WHERE Product_Offer NOT IN ('prod1') GROUP BY Customer_NumberUNION ALLSELECT Customer_Number, SUM(Price) As Total FROM Customer_OfferWHERE Product_Offer IN ('prod2')GROUP BY Customer_Number |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 07:58:13
|
| You are right, after sitting with a calculator I found some discrepancies,I'm trying to get my head around it all lol, Guess the best way to learn is by making the mistakes! |
 |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 08:21:14
|
| Ok, I have managed to cause abit of confusion, Yes I want to sum up totals for a customer and not for a product,Im basically trying to work out the value a customer has! but need to filter this based on a product (I hope this makes sense) Im trying to say Give me the sum of all the purchases for a customer who has bought product a, but not bought product b and c or give me totals for a customer who has bought products b and d but not bought e. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 09:12:00
|
| The next part is I want the totals for customers who have bought certain products say the total for customers who have bought product a and not product b, however if they have bought product c, Id like that added to the total too but it must be mandatory that they have bought a and not bought b |
 |
|
|
soulchyld21
Starting Member
28 Posts |
Posted - 2012-02-09 : 19:23:19
|
| Finally can put this to bed! Thanks to all of you wonderful people, Got it working using UNION ALL Whoop Whoop!! :-D |
 |
|
|
|
|
|
|
|