sql中sum()求和怎么写呀?

Bestregards 2006-10-10 10:18:47
问你下哈,我要把下面一个test表中的字段:
id materialamount
5 10
6 20
5 40
7 30
6 90
要把id相同的materialamount进行相加!
生成test1表,怎么写sql语句呀?
id materialamount
5 50
6 110
7 30
...全文
502 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
no_mIss 2006-10-11
  • 打赏
  • 举报
回复
解决了快给分哇,哇哈哈.
no_mIss 2006-10-11
  • 打赏
  • 举报
回复
select id,sum(materialamount) from test group by id

select distinct id,
(select sum(materialamount) from test where id=t.id)as materialamount
from test t
zhangguangxue_xx 2006-10-11
  • 打赏
  • 举报
回复
忘记加入另一个表了,下面为加上的:
declare @b table(id int,materialamount int)
insert into @b values(5,10)
insert into @b values(6,20)
insert into @b values(5,40)
insert into @b values(7,30)
insert into @b values(6,90)


select id,sum(materialamount) into test1 from @b group by id

结果:
id materialamount
5 50
6 110
7 30

到时候把@b换掉即可。
zhangguangxue_xx 2006-10-11
  • 打赏
  • 举报
回复
declare @b table(id1 int,materialamount int)
insert into @b values(5,10)
insert into @b values(6,20)
insert into @b values(5,40)
insert into @b values(7,30)
insert into @b values(6,90)


select id1,sum(materialamount) from @b group by id1

结果:
5 50
6 110
7 30

chenjunjarysky 2006-10-11
  • 打赏
  • 举报
回复
select id ,sum(materialamount) from t group by id
Bestregards 2006-10-10
  • 打赏
  • 举报
回复
好的!解决了!谢谢各位哈!
MakeToday 2006-10-10
  • 打赏
  • 举报
回复
select id,sum(materialamount) as materialamount into test1
from test
group by id
dawugui 2006-10-10
  • 打赏
  • 举报
回复
select id ,sum(materialamount) from t group by id
九斤半 2006-10-10
  • 打赏
  • 举报
回复
select
ID,
sum(materialamount) as materialamount
into test1 from test
group by ID
九斤半 2006-10-10
  • 打赏
  • 举报
回复
select
ID,
sum(materialamount) as materialamount
from test
group by ID

27,581

社区成员

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

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