Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:28:38+00:00 2026-06-14T05:28:38+00:00

Requests can be made for multiple items and the only key is the surrogate

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:28:39+00:00Added an answer on June 14, 2026 at 5:28 am

    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.

    Try this:

    SELECT item_type as item, COUNT(*)
    FROM purchases 
    WHERE phone_number NOT IN (SELECT DISTINCT phone_number 
                               FROM purchases
                               WHERE item_type = 'popcorn')
    GROUP BY item_type;
    

    SQL Fiddle Demo

    Update: Try this:

    SELECT item_type as item, COUNT(*)
    FROM purchases 
    GROUP BY item_type, phone_number
    HAVING phone_number IN (SELECT DISTINCT phone_number 
                               FROM purchases
                               WHERE item_type = 'popcorn')
      AND item_type <> 'popcorn';
    

    Updated SQL Fiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a service which can process queued requests on a configured
I have a couple of XHR requests, which are handled by the Mootools Request
We use multiple servers to handle incoming web requests which are load-balanced in a
I can send requests to friends using Multi Friend Request Selector inside my Page
I know that in Rails (3.2.2), I can route requests to / with the
How can I scan/record IP requests from a specific application or task? Could I
How can I receive multipart POST data requests in ASP.NET C#?
You can reduce the number of HTTP requests to speed up your site, such
I can use HttpListener to listen for requests coming from the same computer, but
I can use urllib2 to make HEAD requests like so: import urllib2 request =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.