如何实现比例计算

flowerguest 2005-07-10 01:20:48
有如下的表test:
name count
张三 10
李四 4
.
.
.
.
要实现这样的查询:统计出每个NAME所占的COUNT总数比例.
我用的方法是:
declare @allcount int
set @allcount = (select sum(count) from test
select name,count/@allcount as percent from test

现在想用一个查询语句实现以上功能,可以吗?
...全文
115 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
brother2605 2005-07-11


select a.name,(sum(count)/b.count)as percent from
test a cross join (select sum(count)as count from test) b

group by a.name
order by a.name
回复
wangdehao 2005-07-10
select name,(sum(count)+0.0)/(select sum(count) from test)as [percent] from test group by name
回复
filebat 2005-07-10
--你还可以用连接来做
--测试数据与'依帆'相同
select t1.*, percents=1.0*counts/sums
from (select name, counts=sum(count) from test group by name)t1
, (select sums=sum(count)from test)t2
回复
geniusqing 2005-07-10
create table test(name varchar(6),count int)
insert into test
select '张三', 10
union all select '李四', 4

select name,sum(count),(sum(count)+0.0)/(select sum(count) from test) from test group by name

回复
geniusqing 2005-07-10
select name,sum(count),(sum(count)+0.0)/(select sum(count) from test) from test group by name


回复
相关推荐
发帖
应用实例
创建于2007-09-28

2.7w+

社区成员

MS-SQL Server 应用实例
申请成为版主
帖子事件
创建了帖子
2005-07-10 01:20
社区公告
暂无公告