年                  类别               金额2006                  A                220
2006                  B                143
2006                  A                 70
2006                  C                 35
2005                  A                 40现想实现百分比年                  类别              百分比
2006                  A                ?%
2006                  B                ?%      
2006                  C                ?%
2005                  A                ?%
……
就是想统计每年、每一类别的金额占该年全部类别的百分比。            

解决方案 »

  1.   

    --这样?
    declare @t table(年 int,类别 varchar(10),金额 int)
    insert into @t select 2006,'A',220
    union all select 2006,'B',143
    union all select 2006,'A',70
    union all select 2006,'C',35
    union all select 2005,'A',40select a.年,
           a.类别,
           百分比=cast(cast(((sum(a.金额)+0.0)/b.金额)*100 as dec(10,2)) as varchar)+'%'
    from @t a,(select 年,sum(金额) as 金额 from @t group by 年) b
     where a.年=b.年
    group by
     a.年,a.类别,b.金额
    order by a.年 desc