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

Based on a simple test I ran, I don't think it's possible to put
Based on the response to this question: Why does C++ have header files and
Based on this question it appears that the default template for CheckStyle will allow
Based on this question I don't want to litter my ready stuff waiting for
I'm working on an Azure based project for some research and have been running
I have done my research but I am still unable to find a solution
Looking for some guidance on a wcf 4 rest service which is based on
Based on a few posts I've read concerning version control, it seems people think
Based on all my reading there should be one GC thread to invoke all
Based on their work, how do you distinguish a great SQL developer? Examples might

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.