sql 怎么些

ybblack001 2010-08-19 02:14:54
一个表
name source
张三 1
张三 2
李四 3
李四 2
王五 1

-----------
需要得出

name 分数 总分数

张三 3 9
李四 5 9
王五 1 9

求教
...全文
144 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
pigHead_chen 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tomsoncat 的回复:]
没看到最后一列
select name,sum(source),(select sum(source) from table) from table groupby name
[/Quote]
+
czhhlove 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 randomfeel 的回复:]
SQL code
select name,a1,
a2=(select sum(source) from tablename)
from
(select name,sum(source) as a1 from tablename group by name) t
[/Quote]
只需将a1换成分数 a2换成总分数 就行啦
jxmlearner 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 randomfeel 的回复:]
SQL code
select name,a1,
a2=(select sum(source) from tablename)
from
(select name,sum(source) as a1 from tablename group by name) t
[/Quote]
正解!
c慧子 2010-08-26
  • 打赏
  • 举报
回复
写个方法吖。
fucntion f_value(varchar @name)
as
begin
declare @a int
select @a = sum(分数) from 表 where name= @name
reuturns (@a)
end
wwfgu00ing 2010-08-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 scudflying 的回复:]
SQL code
SELECT [name],(SELECT sum([source]) FROM [Table_1] where [name]=a.[name]) as '分数',(SELECT SUM([source]) FROM [Table_1]) as '总分'
FROM [Table_1] as a
group by [name]
[/Quote]
正解
jinguanghai 2010-08-19
  • 打赏
  • 举报
回复
一个人的分数 select sum(分数) from 表名 group by name
总分 select sum(分数) from 表名
cpp2017 2010-08-19
  • 打赏
  • 举报
回复
用多个sql

delcare @TotalScore int
select @TotalScore = sum(source) from T


select name,sum(分数) as 分数,@totalScore as 总分数
from t group by name,@totalScore


porschev 2010-08-19
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ybblack001 的回复:]
唉...为什么不能直接对聚合出来的字段再次聚合呢....郁闷,,,子句好长的,以后维护是个问题...
[/Quote]

这个。。。
ybblack001 2010-08-19
  • 打赏
  • 举报
回复
唉...为什么不能直接对聚合出来的字段再次聚合呢....郁闷,,,子句好长的,以后维护是个问题...
s370092561 2010-08-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 porschev 的回复:]
SQL code


select [name] as 用户名,SUM(source) as 分数,(select SUM(source) from 表名) as 总分数 from 表名 group by [name]
[/Quote]

顶!
scudflying 2010-08-19
  • 打赏
  • 举报
回复
SELECT [name],(SELECT sum([source]) FROM [Table_1] where [name]=a.[name]) as '分数',(SELECT SUM([source]) FROM [Table_1]) as '总分'
FROM [Table_1] as a
group by [name]
ybblack001 2010-08-19
  • 打赏
  • 举报
回复
饿...只能用子表么....实际中这个子表可是有20行....
randomfeel 2010-08-19
  • 打赏
  • 举报
回复
e,兜了个圈
不用这样写的
#6那样就不错
porschev 2010-08-19
  • 打赏
  • 举报
回复


select [name] as 用户名,SUM(source) as 分数,(select SUM(source) from 表名) as 总分数 from 表名 group by [name]

randomfeel 2010-08-19
  • 打赏
  • 举报
回复
select name,a1,
a2=(select sum(source) from tablename)
from
(select name,sum(source) as a1 from tablename group by name) t
tomsoncat 2010-08-19
  • 打赏
  • 举报
回复
没看到最后一列
select name,sum(source),(select sum(source) from table) from table groupby name
ybblack001 2010-08-19
  • 打赏
  • 举报
回复
关键是总分数怎么求
ybblack001 2010-08-19
  • 打赏
  • 举报
回复
没你说那么简单吧...首先要group 然后聚合,但是第三个字段需要group前的聚合结果...
tomsoncat 2010-08-19
  • 打赏
  • 举报
回复
select name,sum(source) from table groupby name
shulin85 2010-08-19
  • 打赏
  • 举报
回复
SELECT SUM[source] FROM [TABLE]
加载更多回复(1)

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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