Requests can be made for multiple items and the only key is the surrogate request_id
I have the following query which returns a result set of 9 digit phone numbers
SELECT phone_number FROM purchases
WHERE item_type = 'popcorn'
GROUP BY phone_number;
I would like to then retrieve a list of of every DISTINCT item_type requested by each of those phone_number that was not ‘popcorn’ and then GROUP BY that item_type. IE I want to know what similar products customers tend to buy.
The desired result set may look something like this
item COUNT(*)
pretzels 200
chips 150
crackers 125
… …
In this case the number of phone_number‘s who bought popcorn and pretzels was 200 (I’m interested in this value). The total number of purchase of pretzels by people who bought popcorn was 1,000 but this is not the value I am interested in.
How can I create a single query that only specifies the item_type and retrieves a result like the above which shows items customers also bought?
Also – If someone can help me make a more descript title that would be appreciated.
EDIT
Here is the table
CREATE TABLE purchases(
request_id BIGINT NOT NULL auto_increment,
phone_number INT(9) NOT NULL,
item_type VARCHAR(256) NOT NULL,
PRIMARY KEY(request_id)
);
EDIT
Request for desired output based on this data http://www.sqlfiddle.com/#!2/23236/2
item COUNT(*)
cc 1
notpopcorn 1
Try this:
SQL Fiddle Demo
Update: Try this:
Updated SQL Fiddle Demo