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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:54:29+00:00 2026-06-15T12:54:29+00:00

What i would like to do is have a Top 10, but the 10th

  • 0

What i would like to do is have a Top 10, but the 10th entry is called “Other” with the sum of everything bar the top 9 within it and has a total. So basically it looks like this:

ReportingDate   FundCode    Currency            Duration Contribution   Percentage
31/10/2012      1111        Malaysian Ringgit   0.5                     14.6
31/10/2012      1111        Turkish Lira        0.3                     13.5
31/10/2012      1111        Russian Rouble      0.5                     11.9
31/10/2012      1111        Indunesian Rupiah   0.6                     11.7
31/10/2012      1111        Mexican Peso        0.6                     11.7
31/10/2012      1111        Polish Zloty        0.3                     10.2
31/10/2012      1111        Mexican Peso        0.4                     10.1
31/10/2012      1111        Polish Zloty        0.3                      9.9
31/10/2012      1111        South African Rand  0.2                      5.8
31/10/2012      1111        Brazilian Real      0.3                      2.0
31/10/2012      1111        Other               0.6                     -1.4
31/10/2012      1111        Total               4.6                    100.0

My code currently looks like this:

;;WITH CTE AS
(
SELECT 
    ReportingDate
,   PortfolioID
,   DV.dmv_nme AS Currency
,   RANK() OVER (PARTITION BY PortfolioID ORDER BY SUM(Percentage) DESC) AS [Rank]
,   ISNULL(CAST(SUM(DurationContribution)/100.0 AS DECIMAL(22,1)),0)    AS [Duration Contribution]
,   CAST(SUM(Percentage) AS DECIMAL(22,1))  AS [Weight]

FROM @Worktable as WT

INNER JOIN dw_domain_value AS DV
    ON DV.dmv_value = WT.Currency
    AND DV.data_cls_num = 2

GROUP BY    WT.ReportingDate
    ,   WT.PortfolioID
    ,   DV.dmv_nme
)

SELECT 
ReportingDate
, PortfolioID
, Currency
, [Rank]
, [Duration Contribution]
, [Weight]
FROM CTE
WHERE [Rank] <= 10
ORDER BY ReportingDate, PortfolioID, [Rank], [Weight] DESC

So this gives me the top 10 fine. So how could i get it so that the final 10th line is “Other” with everything bar the top 9 summed within it, and also include a total at the end?

  • 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-15T12:54:30+00:00Added an answer on June 15, 2026 at 12:54 pm

    For this i decided i couldn’t use CTE, so ended up having to insert these section by section into a temp table as so:

    /* Include only top 9 */
    INSERT INTO @FinalOutput
    SELECT 
            ReportingDate
        ,   PortfolioID
        ,   PortfolioNme
        ,   Currency
        ,   [Rank]
        ,   DurationContribution
        ,   [Weight]
    FROM @WorktableGrouped
    
    WHERE [Rank] <= 9
    
    ORDER BY ReportingDate, PortfolioID, [Rank], [Weight] DESC
    
    /* Aggregate everything outside the top 9 into other */
    INSERT INTO @FinalOutput
    SELECT 
            ReportingDate
        ,   PortfolioID
        ,   PortfolioNme
        ,   'Other'         AS Currency
        ,   10              AS [Rank]
        ,   SUM(DurationContribution) AS DurationContribution
        ,   SUM([Weight])   AS [Weight]
    FROM @WorktableGrouped
    
    WHERE [Rank] > 9
    
    GROUP BY ReportingDate, PortfolioID, PortfolioNme
    
    ORDER BY ReportingDate, PortfolioID, [Rank], [Weight] DESC
    
    SELECT * FROM @FinalOutput
    
    /* Final Select with roll up for total per portfolio */
    SELECT 
        ReportingDate
    ,   PortfolioID
    ,   PortfolioNme
    
    ,   CASE 
            WHEN GROUPING_ID(ReportingDate, PortfolioID, PortfolioNme, Currency, [Rank]) = 3 THEN 'Total'
            ELSE Currency
        END                                                         AS Currency
    
    ,   CASE 
            WHEN GROUPING_ID(ReportingDate, PortfolioID, PortfolioNme, Currency, [Rank]) = 3 THEN 11
            ELSE [Rank]
        END                                                         AS [Rank]
    
    ,   ISNULL(CAST(SUM(DurationContribution) AS DECIMAL(22,1)),0)  AS [Duration Contribution]  
    ,   CAST(SUM([Weight]) AS DECIMAL(22,1))                        AS [Weight]
    --, GROUPING_ID(ReportingDate, PortfolioID, PortfolioNme, Currency, [Rank])
    
    FROM @FinalOutput
    
    GROUP BY ReportingDate
    , PortfolioID
    , PortfolioNme
    , Currency
    , [Rank] WITH ROLLUP
    
    HAVING GROUPING_ID(ReportingDate, PortfolioID, PortfolioNme, Currency, [Rank]) IN (0,3)
    
    ORDER BY ReportingDate, PortfolioID, [Rank]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a header on top of a ListView , but
So in my project i would like have a nice treeview that has images.
I would like to have a layered window that is always-on-top, which I can
I would like to use a Facelets template within another template. Currently I have
So, I would like to have 2 buttons Next and Previous, which has to
I would like to have a global toolbar at the top in each view
I would like to have the topLinks within Magento CE 1.6 displayed within a
I have a top-level page called ReceiveItem . Within that page, I have a
I would like to have a UISearchBar on the top of my UITableView which
I would like to have sections of a site so the top menu is

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.