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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:13:48+00:00 2026-05-26T08:13:48+00:00

Need to build a Top 10 query that works with SQLite and Postgres. Client

  • 0

Need to build a “Top 10” query that works with SQLite and Postgres.

Client model
Client has_many :merchandises, :through => :orders, :source => :items

I want to group merchandises ordered by product_id, get the count for each and sort by most product ordered top and limit to 10.

Client.last.merchandises.group(:product_id).count(:quantity)
SELECT COUNT("items"."quantity") AS count_quantity, product_id AS product_id FROM "items" INNER JOIN "orders" ON "items"."order_id" = "orders"."id" WHERE "orders"."client_id" = 2 GROUP BY product_id
=> {1=>91, 2=>1, 12=>1, 32=>1, 33=>1, 34=>1, 37=>1, 75=>1, 84=>1, 85=>1}

What’s missing: sort by, limit to 10 and get product.name along with quantity count

Latest development:

Items are selected but need to show product.name

class Client < ActiveRecord::Base 
def top_ten_products
  Item.find_by_sql(
    "SELECT i.product_id, sum(i.quantity) AS sum_quantity
    FROM   orders o
    JOIN   items i ON i.order_id = o.id 
    WHERE  o.client_id = 2 
    GROUP  BY 1
    ORDER  BY 2 DESC
    LIMIT  10;"
   )
 end

Console output

=> [#<Item product_id: 1>, #<Item product_id: 37>, #<Item product_id: 75>, #<Item product_id: 12>, #<Item product_id: 32>, #<Item product_id: 33>, #<Item product_id: 2>, #<Item product_id: 34>, #<Item product_id: 84>, #<Item product_id: 85>] 

Client#show

<%= @client.top_ten_products %>
  • 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-26T08:13:48+00:00Added an answer on May 26, 2026 at 8:13 am

    Assuming that product_id is a column of table items, the query could look like this in PostgreSQL:

    SELECT i.product_id
          ,p.name
          ,sum(i.quantity) AS sum_quantity
    FROM   orders o
    JOIN   items i ON i.order_id = o.id 
    LEFT   JOIN product p USING (product_id)
    WHERE  o.client_id = 2 
    GROUP  BY 1,2
    ORDER  BY 3 DESC, 2 -- with same quantity, order by name
    LIMIT  10;
    

    Note:

    I changed your quantity aggregation to a sum and added a commented column for count, as I suspect you mistakenly had a count where you want a sum.

    Edit2:

    sum() is confirmed.
    Included name from table product per request.
    LEFT JOIN is just a precaution for missing entries in table product, if referential integrity is guaranteed, it can be a plain JOIN instead.

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

Sidebar

Related Questions

I need to build a django project on top of an existing database. I'll
I need to build some client side code which follows this use case: An
I need to build a configuration panel in the Plone control panel that store
So I have a query that is a Top Nth aggregate query, and I
I need to build an application on Mac OS X that runs on the
I need to build some searching logic that produces ranked results. A simple example
I need to build a site in HTML. I need the top of every
I need to build a page that looks like Mac Mail or an Outlook
I need to build a page that is scrolling up and down after x
I need a way to push a project to the top of the build

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.