如何实现比例计算

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

现在想用一个查询语句实现以上功能,可以吗?
...全文
203 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
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


27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧