2

我正在运行一个按国家/地区划分百分比的查询.. 像这样:

 select country_of_risk_name, (sum(isnull(fund_weight,0)*100)) as 'FUND WEIGHT' from       OFI_Country_Details
WHERE FIXED_COMP_FUND_CODE = 'X'
GROUP BY country_of_risk_name

这将返回正确的输出。范围可以从 1 个国家到 100 个国家。我如何编写我的逻辑,它显示前 5 个最高百分比,然后将前 5 名之外的所有那些分组到“其他”类别中?示例输出:

  1. 美国 - 50%
  2. 加拿大 - 10%
  3. 法国 - 4%
  4. 西班牙 - 2%
  5. 意大利 - 1.7%
  6. 其他 - 25%
4

4 回答 4

2
SELECT rn, CASE WHEN rn <= 5 THEN x.country_of_risk_name
                ELSE 'Other' END AS country_of_risk_name, 
           SUM(x.[FUND WEIGHT]) AS SumPerc
FROM(      
     SELECT country_of_risk_name,
            CASE WHEN ROW_NUMBER() OVER(ORDER BY SUM(ISNULL(fund_weight,0)*100) DESC) <= 5
                 THEN ROW_NUMBER() OVER(ORDER BY SUM(ISNULL(fund_weight,0)*100) DESC)
                 ELSE 6 END AS rn,
            SUM(ISNULL(fund_weight,0)*100) AS [FUND WEIGHT]
     FROM country_of_risk_name
     WHERE FIXED_COMP_FUND_CODE = 'X'
     GROUP BY country_of_risk_name
     ) x
GROUP BY rn, CASE WHEN rn <= 5 THEN x.country_of_risk_name
                  ELSE 'Other' END 
ORDER BY x.rn

见演示SQLFiddle

于 2013-06-28T06:01:12.813 回答
0

这是一种仅使用 SQL 的丑陋方法:

select country, sum(perc)
from (
  select 
    case when rn <= 5 then country else 'Other' end 'Country',
    case when rn <= 5 then rn else 6 end rn,    
    perc
  from (
    select *, row_number() over (order by perc desc) rn
    from yourresults
    ) t
) t
group by country, rn
order by rn

我已将其用作yourresults上述查询的结果-将其放入 a 中Common Table Expression,您应该很高兴:

with yourresults as (
    select country_of_risk_name, (sum(isnull(fund_weight,0)*100)) as 'FUND WEIGHT' 
    from OFI_Country_Details
    where FIXED_COMP_FUND_CODE = 'X'
    group by country_of_risk_name
)
...
于 2013-06-28T03:28:31.243 回答
0

尝试这个。

    declare  @country_of_risk_name as table (country varchar(100),
                              FUND decimal(10,2));

    insert into @country_of_risk_name values
      ('USA', 50),
      ('Canada', 10),
      ('France', 4),
      ('Spain', 2),
      ('Italy', 1.7),
      ('Other1', 1.5),
      ('Other2', 1.5),
      ('Other3', 1.5),
      ('Other4', 1.5),
      ('Other5', 1.5),
      ('Other6', 1.5);



    DECLARE @TBL AS TABLE(country_of_risk_name VARCHAR(100), FUND DECIMAL(18,2))

    INSERT INTO @TBL
    SELECT TOP 5 country,SUM(FUND) FROM @country_of_risk_name
      group by country
      ORDER BY SUM(FUND) desc
      select * from @TBL
      UNION ALL 
      select 'other', SUM(tbl1.fund) 
      from @country_of_risk_name tbl1 
      left join @TBL tbl2 on tbl1.country =tbl2.country_of_risk_name 
      where 
          tbl2.country_of_risk_name  is null 
于 2013-06-28T03:33:08.763 回答
0

这是使用 CTE 的查询:也是一个工作解决方案示例

;with TotalPercent(Country_Of_Risk_Name, Fund_Weight)
as(
    select Country_Of_Risk_Name, sum(isnull(Fund_Weight, 0) * 100) Fund_Weight
    from OFI_Country_Details
    where
       FIXED_COMP_FUND_CODE = 'X'
    group by Country_Of_Risk_Name
  ),
  PercentWithRank(Country_Of_Risk_Name, Fund_Weight, RowNumber)
  as (
      select Country_Of_Risk_Name, Fund_Weight, row_number() over (order by Fund_Weight desc) RowNumber
      from TotalPercent
  )
  select Country_Of_Risk_Name, Fund_Weight
  from PercentWithRank
  where
      RowNumber <= 5
  union all
  select 'Other', sum(Fund_Weight) Fund_Weight
  from PercentWithRank
  where
      RowNumber > 5
于 2013-06-28T03:39:32.237 回答