Finding the top 3 for each the Year

Posted on 2011-10-30
Last Modified: 2012-05-12
I have a dataset that looks something like this...

Product Name,  Manufacturer,  SaleYear,   GrossSales

I need to find the top three products sold by GrossSales for each year, so it would look like this...

Sony, Product X, $20,000
HP, Product Y, $19,500
Dell, Product Q,  $18,000

Apple, Product J,  $22,150
Dell, Product Q,   $19,200
Sony, Product X,  $14,000

2010... three products....


So for each year, show the top three products sold by the Gross Revenue...

I believe this uses Row_Number() and Over but not sure of the format.

Question by:gdemaria
    LVL 39

    Accepted Solution

    try this

    Select Product Name,  Manufacturer,  SaleYear,   GrossSales
    From (Select Product Name,  Manufacturer,  SaleYear,   GrossSales,
    row_number() over(partition by SaleYear order by GrossSales desc) rowNum
    from tableName) as A
    where rowNum <=3
    LVL 39

    Author Closing Comment

    That was it, thanks very much!

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now