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 9241247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:20:14+00:00 2026-06-18T08:20:14+00:00

I have a table that has 2 columns that I’m concerned with Business_ID /

  • 0

I have a table that has 2 columns that I’m concerned with

Business_ID   /   Product type

I am trying to write a SQL query to only get businesses that sell one type, but not the others. So the table is populated with

B_ID    prod_type
123  |   A
123  |   A
123  |   B
234  |   A
234  |   C
234  |   C
456  |   A
456  |   D
789  |   A

and the list goes on and on. I am trying to write a SQL statement that will find a B_ID that sells prod_type A and not prod_type B, C, D. Here is what I’m trying but it doesn’t work

SELECT phop_1.Business_id, phop_1.PRODUCT_TYPE, count(*) 
FROM phop phop_1, phop phop_2
WHERE phop_1.Business_id = phop_2.Business_id
  AND phop_1.PRODUCT_TYPE = 'A'
  AND NOT phop_2.PRODUCT_TYPE = 'B'
GROUP BY phop_1.Business_id, phop_1.PRODUCT_TYPE

I also found how to exclude using an outer join, but since it’s the same table I can’t do that unless there is a way to select out the prod_type A before doing the join.

select phop_1.Business_id, phop_1.PRODUCT_TYPE, count(*) 
from phop phop_1
LEFT OUTER JOIN phop phop_2
ON phop_1.Business_id = phop_2.Business_id
WHERE phop_2.Business_id IS NULL
GROUP BY phop_1.Business_id, phop_1.PRODUCT_TYPE
  • 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-18T08:20:15+00:00Added an answer on June 18, 2026 at 8:20 am

    You can try something like this:

    SELECT DISTINCT p.BusinessId
    FROM phop p
       LEFT JOIN phop p2 ON p.BusinessId = p2.BusinessId AND p2.Product_Type <> 'A'
    WHERE p2.BusinessId IS NULL
    

    And the Fiddle.

    Good luck.

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

Sidebar

Related Questions

I need to write the following in SQL: I have a table that has
In sql server 2008, I have a table that has the columns [a],[b],[c],[sort] and
I have a table that has 2 columns: FruitID, FruitSize. I want to write
I have a gridview table that has three columns..fileID, uploadedBy and delete. Only the
I have a table that has two columns: CommunityID PersonID And A People table
I have a table that has 5 columns: AcctId (int), Address1 (varchar), Address2 (varchar),
Screenshot mockup: http://tinypic.com/r/y2qex/5 Problem: I have a table that has 53 columns; one for
I have a table that has two datetime columns (one for start time and
I have a mysql table that has 2 columns - Column 1 contains a
I have a city table that has two columns from_city to_city now lets assume

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.