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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:16:55+00:00 2026-06-11T15:16:55+00:00

I am relatively new to writing SQL. I have a requirement where I have

  • 0

I am relatively new to writing SQL. I have a requirement where I have to display the top 5 records as it is and consolidate the rest as 1 single record and append it as the 6th record. I know top 5 selects the top 5 records, but I am finding it difficult to put together a logic to consolidate the rest of the records and append it at the bottom of the result set.

weekof          sales    year    weekno
-------------------------------------------------------------
07/01 - 07/07   2   2012    26  
07/08 - 07/14   2   2012    27  
07/29 - 08/04   1   2012    30  
08/05 - 08/11   1   2012    31  
08/12 - 08/18   32  2012    32  
08/26 - 09/01   2   2012    34  
09/02 - 09/08   8   2012    35  
09/09 - 09/15   46  2012    36   
09/16 - 09/22   26  2012    37

I want this to be displayed as

weekof          sales
----------------------
09/16 - 09/22   26  
09/09 - 09/15   46  
09/02 - 09/08   8   
08/26 - 09/01   2   
08/12 - 08/18   32  
07/01 - 08/11   6
  • 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-11T15:16:56+00:00Added an answer on June 11, 2026 at 3:16 pm

    Except when weekof spans years, this will get the data you want and in the correct order:

    ;WITH x AS 
    (
      SELECT weekof, sales, 
        rn = ROW_NUMBER() OVER (ORDER BY [year] DESC, weekno DESC) 
      FROM dbo.table_name
    )
    SELECT weekof, sales FROM x WHERE rn <= 5
    UNION ALL
    SELECT MIN(LEFT(weekof, 5)) + ' - ' + MAX(RIGHT(weekof, 5)), SUM(sales)
    FROM x WHERE rn > 5    
    ORDER BY weekof DESC;
    

    When the rows being returned span a year, you may have to return the rn as well (and just ignore it at the presentation layer):

    ;WITH x AS 
    (
      SELECT weekof, sales, 
        rn = ROW_NUMBER() OVER (ORDER BY [year] DESC, weekno DESC) 
      FROM dbo.table_name
    )
    SELECT weekof, sales, rn FROM x WHERE rn <= 5
    UNION ALL
    SELECT MIN(LEFT(weekof, 5)) + ' - ' + MAX(RIGHT(weekof, 5)), SUM(sales), rn = 6 
    FROM x WHERE rn > 5
    ORDER BY rn;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to PHP and have been writing a project using what I
I am relatively new with Django and it's ecosystem. I am writing REST api
We have a relatively new database project that we have been writing views and
I'm relatively new to Perl, writing some XML parsing scripts. I have done two
I am relatively new to writing in C. I have self taught myself using
Im relatively new to the Android world but I have a quick question. I
I'm relatively new to Objective C and while I've been trucking right along writing
I am relatively new to Python, and I am experimenting with writing the following
I'm relatively new to C# so please bear with me. I am writing a
I am relatively new to C# so please bear with me. I am writing

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.