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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:44:27+00:00 2026-05-28T02:44:27+00:00

Greeting all. I have a SQL 2008 express database, lets name is tbl_Merchant, similar

  • 0

Greeting all. I have a SQL 2008 express database, lets name is tbl_Merchant, similar as following:

Merchant | Sales | Month
Comp.1      100     1
Comp.2      230     1
Comp.3      120     1
Comp.1      200     2
Comp.2      130     2
Comp.3      240     2
Comp.1      250     3
  .          .      .
  .          .      .
  .          .      .

I need to find the top 10 merchant with sales every month over 12 months.

It is very easy if it is just one month.

SELECT TOP 10 
       Merchant, 
       Sales, 
       Month
FROM   tbl_Merchant
WHERE  Month = 1
ORDER BY Sales DESC

But I am stuck if I wan to find them over 12 months. I need to display 120 merchants, which are top 10 sales merchant of each month. Currently my solution is to union 12 tables from month 1 to 12 together but I don’t think it is a good way.

Can any one kindly give me any suggestion?

Thank you very much.

  • 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-28T02:44:27+00:00Added an answer on May 28, 2026 at 2:44 am

    Valid for Sql Server 2005+

    Try this for overall:

    SELECT TOP 10 
           Merchant, 
           SUM(Sales) Sales
    FROM   tbl_Merchant
    WHERE  Month BETWEEN 1 and 12
    GROUP BY Merchant
    ORDER BY 2 DESC
    

    OR

    if you need details per month for top 10 PER MONTH

    ;WITH MonthsCTE(m) as
    (
        SELECT 1 m
        UNION ALL 
        SELECT m+1
        FROM MonthsCTE
        WHERE m < 12
    )
    SELECT m [Month], t.*
    FROM MonthsCTE
    CROSS APPLY 
    (
        SELECT TOP 10
           Merchant, 
           SUM(Sales) Sales
        FROM   tbl_Merchant
        WHERE  Month = MonthsCTE.m
        GROUP BY Merchant
        ORDER BY 2 DESC
    ) t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greeting all. I have a SQL Server 2008 Express database with a table called
Greeting all... i have two table and their structure is identical... Table logDetail Date
greeting all i am working on school project and i have a problem with
Greetings All, I have a SQLite database that I'm trying to add a new
Greetings All; I have a desktop java application which gives the following output in
Greetings all, I have a question. On my MOSS 2007 dev box, I created
Greetings all, I have a question. I am trying to build a parametrized query
Greetings to all! This is my first question here on stackoverflow. I have a
Greetings, stack overflow In my database, I already have one table, 'contacts' that contains
Say I have the following custom component: <s:Group xmlns:fx=http://ns.adobe.com/mxml/2009 xmlns:s=library://ns.adobe.com/flex/spark xmlns:mx=library://ns.adobe.com/flex/mx> <fx:Script> <![CDATA[ [Bindable]

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.