select 公司,a.datetime 时间,sum(a.lr)/b.sumlr 利润比率
from table_1 a left join (select datetime,sum(isnull(lr,0)) as sumlr from table_1 group by datetime) b on a.datetime=b.datetime
group by ,a.datetime,b.sumlr
x 09 0.166666
x 10 0.222222
x 11 0.250000
y 09 0.333333
y 10 0.333333
y 11 0.333333
z 09 0.500000
z 10 0.444444
z 11 0.416666
--测试数据
drop table test
go
create table test
(g char(1),y char(2),gx int)
go
insert test
select
'x','09', 1
union select
'x','10', 2
union select
'x','11', 3
union select
'y','09', 2
union select
'y','10', 3
union select
'y','11', 4
union select
'z','09', 3
union select
'z','10', 4
union select
'z','11', 5
go
--
select a.g,a.y,convert(varchar(20),a.gx*100.00/(select sum(gx) from test where y=a.y))+'%' 贡献率 from test a
go
/*结果
x 09 16.6666666666666%
x 10 22.2222222222222%
x 11 25.0000000000000%
y 09 33.3333333333333%
y 10 33.3333333333333%
y 11 33.3333333333333%
z 09 50.0000000000000%
z 10 44.4444444444444%
z 11 41.6666666666666%
*/