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

  • Home
  • SEARCH
  • 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 7887595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:37:15+00:00 2026-06-03T05:37:15+00:00

Situation: I have a database that contains sales records, each record has a ID

  • 0

Situation:

I have a database that contains sales records, each record has a ID (PK), ProductCode, Year, Month, SalesVolume.

As a user I will specify the year, so if I specify 1980, I will query the database for all the records that correspond to that year.

I am trying to create a query that will total the months SalesVolumes for the specified year, then pick the top 5 sales volume values.

What I have gathered so far is to somehow do the above, then to choose the top 5 sales volumes values, put them in descending order, and select the top 5, but that’s as far as I have got

Note in a year there will be SalesVolumes for multiple products:

ID,     Product Code, Year, Month, SalesVolume
23041   121           1980  1      21
23042   121           1980  2      960
23043   121           1980  3      939
23044   121           1980  4      927
23045   121           1980  5      931
23046   121           1980  6      950
23047   121           1980  7      975
23048   121           1980  8      994
23049   121           1980  9      994
23050   121           1980  10     968
23051   121           1980  11     918
23052   121           1980  12     854
23425   122           1980  1      1002
23426   122           1980  2      1032
23427   122           1980  3      1090
23428   122           1980  4      1062
23429   122           1980  5      1010
23430   122           1980  6      1103
23431   122           1980  7      1214
23432   122           1980  8      1122
23433   122           1980  9      1019
23434   122           1980  10     1181
23435   122           1980  11     1343
23436   122           1980  12     1180

Expected Output:

For 1980

Product Code, SalesVolume
121         , (total SalesVolume of the 12 months)
122         , (total SalesVolume of the 12 months)
  • 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-03T05:37:16+00:00Added an answer on June 3, 2026 at 5:37 am

    Here’s what you can do – total up the sales for each item by for a given year, rank them, and then select the top 5 ranking items.

    SELECT *
    FROM (
        SELECT "Product Code",
            SUM(SalesVolume) as total,
            RANK() OVER (ORDER BY SUM(SalesVolume) DESC) as rnk
        FROM YourTable
        WHERE Year = 1980
        GROUP BY "Product Code"
    ) A
    WHERE rnk <= 5
    

    If you want to do select the top 5 per year for all years, you can do that too:

    SELECT *
    FROM (
        SELECT Year,
            "Product Code",
            SUM(SalesVolume) as total,
            RANK() OVER (PARTITION BY Year ORDER BY SUM(SalesVolume) DESC) as rnk
        FROM YourTable
        GROUP BY Year, "Product Code"
    ) A
    WHERE rnk <= 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the situation I'm in: We have a field in our database that contains
I have a database that has two tables, one of which contains a foreign
I have situation in which I read a record from a database. And if
I have this situation: I have an entity, Person , that contains all personal
I have a situation where I have a table that has a soft foreign
So here is the situation. I have a database on a remote server that
I have a database schema that contains multiple tables with different names, but all
I have a table in a database that contains a variety of paths to
I have strange situation - when I'm trying to update model, that can contains
Current situation: I have a database with following structure: Product Category (contains column product_id)

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.