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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:40:21+00:00 2026-06-13T12:40:21+00:00

I have a stored procedure that returns a set of rows for each ActivityUnitId

  • 0

I have a stored procedure that returns a set of rows for each ActivityUnitId.

What is needed is the Top 10 rows for each ActivityUnitId based on Hours. I have managed to achieve this, my query looks like

With Data AS
(SELECT  
   ProjectID,ActivityUnitID,Description,GroupID,
   SUM(Hours) AS NoiseHours,
   ROW_NUMBER() OVER(PARTITION BY ActivityUnitID ORDER BY SUM(Hours) DESC) as 'RowNum'
FROM         
   tbl_Sub 
INNER JOIN
   tbl_AnalysisData ON tbl_Sub.SubActivityID = tbl_AnalysisData.SubActivityID 
     INNER JOIN
       tbl_Analysis ON tbl_AnalysisData.LookupID = tbl_Analysis.LookupID
         INNER JOIN
           tbl_ActivityUnit ON tbl_ActivityUnit.ActivityUnitID = tbl_Sub.ActivityUnitID
           tbl_Suby.ProjectID = @ProjectID 
         AND
           tbl_Sub.ActivityUnitID = ISNULL(@ActivityUnitID,tbl_Sub.ActivityUnitID)
GROUP BY 
  ActivityUnitID,ProjectID,  Description,AUGroupID
) SELECT * from Data where RowNum<=10

The ‘RowNum’ column contains Row number for each row, value of which has been assigned based to hours. So the First 10 rows contain the rows with top 10 hours.

Now what I want is to add an Extra summary Row in the end for each ActivityUnitID. This will contain the sum of Hours for all the rows that have been left out i.e. summary Row for
RowNum > 10

So what I’ll have in the end is Top 10 rows for each ActivityUnitID + an extra row that would sum up the hours for other rows for that ActivityUnitID

For example lets say I have a table with 2 cols

ID  Hours     RowNum
1A    30         1
2B    20         2
3C    10         3
4D     5         4
5E     4         5
6F     3         6

How do I do a select on this so that I get Rows where RowNum <=3 and another row With summation of others

ID  Hours  
1A   30
2B   20
3C   10
Oth  12
  • 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-13T12:40:22+00:00Added an answer on June 13, 2026 at 12:40 pm

    The simplest way would be to UNION those totals with the data you already have

    SELECT  NULL, NULL, 'Totals', NULL, SUM(NoiseHours), 11 AS RowNumber
    FROM    Data 
    WHERE   RowNum > 10
    

    Your complete statement could then look something like this

    With Data AS
    (
    SELECT  ProjectID,ActivityUnitID,Description,GroupID,
            SUM(Hours) AS NoiseHours,
            ROW_NUMBER() OVER(PARTITION BY ActivityUnitID ORDER BY SUM(Hours) DESC) as 'RowNum'
    FROM         tbl_Sub 
                INNER JOIN
                tbl_AnalysisData ON tbl_Sub.SubActivityID = tbl_AnalysisData.SubActivityID 
                INNER JOIN
                tbl_Analysis ON tbl_AnalysisData.LookupID = tbl_Analysis.LookupID
                INNER JOIN
                tbl_ActivityUnit ON tbl_ActivityUnit.ActivityUnitID = tbl_Sub.ActivityUnitID
    tbl_Suby.ProjectID = @ProjectID 
    AND
    tbl_Sub.ActivityUnitID = ISNULL(@ActivityUnitID,tbl_Sub.ActivityUnitID)
    GROUP BY ActivityUnitID,ProjectID,  Description,AUGroupID
    )
    SELECT * from Data where RowNum<=10
    UNION ALL 
    SELECT  NULL, NULL, 'Totals', NULL, SUM(NoiseHours), 11 AS RowNumber
    FROM    Data 
    WHERE   RowNum > 10
    

    The difficulty when using a union is that both parts of the union should have the same amount and same type of columns, hence the need for adding dummy columns to the select.

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

Sidebar

Related Questions

I have a MYSQL stored procedure SP1() that returns a result set. I want
I have a stored procedure that returns a result set (4 columns x n
I have a stored procedure that, depending on the input, returns multiple rows of
I have a stored procedure that returns data in the format of: Date |
I have a stored procedure that returns a result with 250!!! columns. But I
I have the following iBatis mapping for an Oracle Stored Procedure that returns a
I have a stored procedure that takes no parameters, and it returns two fields.
Let's say I have a stored procedure in Microsoft SQL Server 2005 that returns
I have a function that returns a date from a stored procedure, and it
I have a Stored Procedure that returns three record sets. Here it is being

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.