declare @sum numeric(10,2)
select @sum=sum(收入) from t
select 代号,收入=sum(收入),总收入=@sum,占比=cast(sum(收入)/@sum as numeric(5,2))
from t group by 代号
select @sum=sum(收入) from t
select 代号,收入=sum(收入),总收入=@sum,占比=cast(sum(收入)/@sum as numeric(5,2))
from t group by 代号
insert into t select 1001, 200.00, '2004-11-7'
insert into t select 1002, 150.00, '2004-11-6'
insert into t select 1002, 250.00, '2004-11-7'
insert into t select 1003, 100.00, '2004-11-7'select * from t
select 代号,sum(收入)as 收入,总收入=(select sum(收入)from t),cast(sum(收入)*1.0/(select sum(收入)from t)as decimal(10,1))as 占比 from t group by 代号drop table t
from amt group by no
1001 200.00 2004-11-7
1002 150.00 2004-11-6
1002 250.00 2004-11-7
1003 100.00 2004-11-7===================================================================现在要得到如下表:代号 收入 总收入 占比
------------------ ------------------- --------------- --------------------
1001 500.00 1000.00 0.5
1002 400.00 1000.00 0.4
1003 100.00 1000.00 0.1
select 代号,sum(收入),总收入,sum(收入)/总收入 as 占比 from
(select sum(收入) as 总收入 from table_name) a,table_name b
group by 代号,总收入;