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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:18:48+00:00 2026-06-05T20:18:48+00:00

I have a table, called Prices composed of a few fields, two of which

  • 0

I have a table, called Prices composed of a few fields, two of which is ProductId and PriceType. Some products have both Wholesale price (PriceType = W) and Retail price (PriceType = R), i.e. they have two records with the same product id.

I would like to display to the user those products with no retail price (i.e. those with only one record W).

How should I write my query to do this?

I have used something like

Select * from Products 
INNER JOIN Prices ON Products.Id = Prices.ProductId 
WHERE ProductId NOT IN 
  (SELECT ProductId FROM Prices WHERE PriceType="R") 

but this is executing very slow. I have checked and verified that the part taking a long time is really the NOT IN clause.

Any ideas?

  • 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-05T20:18:49+00:00Added an answer on June 5, 2026 at 8:18 pm
    SELECT * 
    FROM Products 
    INNER JOIN Prices 
            ON Products.Id = Prices.ProductId 
           AND Prices.PriceType = 'W'
    

    This will filter out the Prices at the join so you never have any PriceType='R' records joined.

    To filter out those records that do not have an ‘R’ at all, you can use the following query, which removes the sub-select on your where clause

    SELECT 
        Products.ProductId
    FROM 
        Products 
        INNER JOIN Prices A
            ON Products.ProductId = A.ProductId 
            AND A.PriceType = 'W'
        LEFT JOIN Prices B
            ON A.ProductId = B.ProductId 
            AND B.PriceType <> A.PriceType    
    WHERE
        A.PriceType = 'W'
        AND B.PriceId is null
    

    To ensure this runs as fast as possible, ensure you add an index to your Prices table that includes both the ProductId and PriceType columns

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

Sidebar

Related Questions

I have a table called products, which contains two rows (plus others):price and mfr.
I have a table called prices which includes the closing price of stocks that
I have a table called 'prices with the following structure: 'id', 'price, 'availability_id' and
I have a table called products containing a field called price and I simply
I have a table called order which contains columns id , user_id , price
i have table called as Support, which have a field named Name and contains
I have two tables, one called product_text with a products name, description and category.
I have a SQLite table called Price like this: date, productId, price ---------------------- 20120601,
Hay, I have a table a products which has a 'cost_price' field. When a
I have a simple table called that contains share prices in MySQL: Table `share_prices`

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.