注册 登录
编程论坛 SQL Server论坛

一个SQL查询问题

中大奖 发布于 2012-04-20 16:20, 744 次点击
表a 里有code(人员编码)和name(人员姓名),表b里code(人员编码)和 amount(消耗物品数量)。怎么写语句才能得出name和sum(amount),请赐教,万分感谢。
9 回复
#2
soosii2012-05-10 20:25
select DISTINCT name ,sum(amount)from a,b where a.code=b.code
你试下
#3
autobot2012-05-15 09:26
select a.name,sum(b.amount) from a inner join b on a.code = b.code group by a.name
#4
netlin2012-05-21 23:39
3楼是正解,不过我补充一点:
select a.name,isnull(sum(b.amount),0) as tatol
    from a left join b on a.code = b.code
    group by a.name

这样,就可以把全部人员的消耗物品数量表出来,包括消耗物品数量为0的人员。
#5
shangxisen2012-06-05 00:15
select a.name,sum(b.amount) from a ,b where a.code = b.code group by a.name
#6
一滴血2012-06-06 19:30
select name,amount
from a,b
where a.code=b.code and b.amount=sum(amount)
#7
一滴血2012-06-06 19:39
select name,amount
from a,b
where a.code=b.code and b.amount=sum(amount)
#8
佳L2012-06-06 19:44
回复 7楼 一滴血
你这个查询应该是错的吧?
#9
songssong2012-06-09 08:58
三楼写的对。
sql 语句真的很有趣。
#10
jhdycw2012-06-26 15:57
初学从那里开始?
1