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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:37:30+00:00 2026-05-20T17:37:30+00:00

Based on my research, this is a very common problem which generally has a

  • 0

Based on my research, this is a very common problem which generally has a fairly simple solution. My task is to alter several queries from get all results into get top 3 per group. At first this was going well and I used several recommendations and answers from this site to achieve this (Most Viewed Products). However, I’m running into difficulty with my last one “Best Selling Products” because of multiple joins.

Basically, I need to get all products in order by # highest sales per product in which the maximum products per vendor is 3 I’ve got multiple tables being joined to create the original query, and each time I attempt to use the variables to generate rankings it produces invalid results. The following should help better understand the issue (I’ve removed unnecessary fields for brevity):

Product Table

productid | vendorid | approved | active | deleted

Vendor Table

vendorid | approved | active | deleted

Order Table

orderid | `status` | deleted

Order Items Table

orderitemid | orderid | productid | price

Now, my original query to get all results is as follows:

SELECT COUNT(oi.price) AS `NumSales`, 
       p.productid, 
       p.vendorid
FROM products p
INNER JOIN vendors v ON (p.vendorid = v.vendorid)
INNER JOIN orders_items oi ON (p.productid = oi.productid)
INNER JOIN orders o ON (oi.orderid = o.orderid)
WHERE (p.Approved = 1 AND p.Active = 1 AND p.Deleted = 0)
AND (v.Approved = 1 AND v.Active = 1 AND v.Deleted = 0)
AND o.`Status` = 'SETTLED'
AND o.Deleted = 0
GROUP BY oi.productid
ORDER BY COUNT(oi.price) DESC
LIMIT 100;

Finally, (and here’s where I’m stumped), I’m trying to alter the above statement such that I received only the top 3 product (by # sold) per vendor. I’d add what I have so far, but I’m embarrassed to do so and this question is already a wall of text. I’ve tried variables but keep getting invalid results. Any help would be greatly appreciated.

  • 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-05-20T17:37:30+00:00Added an answer on May 20, 2026 at 5:37 pm

    Even though you specify LIMIT 100, this type of query will require a full scan and table to be built up, then every record inspected and row numbered before finally filtering for the 100 that you want to display.

    select
        vendorid, productid, NumSales
    from
    (
        select
            vendorid, productid, NumSales,
            @r := IF(@g=vendorid,@r+1,1) RowNum,
            @g := vendorid
        from (select @g:=null) initvars
        CROSS JOIN 
        (
            SELECT COUNT(oi.price) AS NumSales, 
                   p.productid, 
                   p.vendorid
            FROM products p
            INNER JOIN vendors v ON (p.vendorid = v.vendorid)
            INNER JOIN orders_items oi ON (p.productid = oi.productid)
            INNER JOIN orders o ON (oi.orderid = o.orderid)
            WHERE (p.Approved = 1 AND p.Active = 1 AND p.Deleted = 0)
            AND (v.Approved = 1 AND v.Active = 1 AND v.Deleted = 0)
            AND o.`Status` = 'SETTLED'
            AND o.Deleted = 0
            GROUP BY p.vendorid, p.productid
            ORDER BY p.vendorid, NumSales DESC
        ) T
    ) U
    WHERE RowNum <= 3
    ORDER BY NumSales DESC
    LIMIT 100;
    

    The approach here is

    1. Group by to get NumSales
    2. Use variables to row number the sales per vendor/product
    3. Filter the numbered dataset to allow for a max of 3 per vendor
    4. Order the remaining by NumSales DESC and return only 100
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently carrying out research for the design and implementation of a web-based
I have a very simple and standard PHP force download script. How do I
Im currently making a very simple WebApp on a Website using ASP.NET Framework using
I am very new to web development and the use of Open Source. I
I am working on a personal project based in PHP and MySQL, and I
I have been asked to add support to a xml based website. I have
There is a number of Flash and AJAX based uploaders around. I need a
I am very new to Android (and Java) originally a C# developer.. and am
As far as I know I've never encountered a SHOULD construct in a computer
I'm looking for a way to detect character sets within documents. I've been reading

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.