日期:2014-05-18  浏览次数:20582 次

SQL查询的问题
待操作表结构
A表:
name   countfield1   countfield2
张三     15                             -1
赵四     10                             0
张三     1                               0
张三     2                               0
赵四     5          0
得到一个表以统计同名字的countfield1及countfield2字段值的总和,即如下所示
name   countfield
张三     17
赵四     15
如何写SQL查询语句得到或如何写存储过程以供调用?

------解决方案--------------------
select name,sum(countfield1+countfield2) from [table] group by name
------解决方案--------------------
select [name],sum(isnull(countfield1,0)+isnull(countfield2,0)) as countfield
from a
group by [name]
------解决方案--------------------
select count(countfield1+countfield2 ) from A group by name
------解决方案--------------------
create table tab(name varchar(10),countfield1 int,countfield2 int)
insert tab
select '张三 ',15,-1 union all
select '赵四 ',10,0 union all
select '张三 ',1,0 union all
select '张三 ',2,0 union all
select '赵四 ',5,0
select name,sum(countfield1+countfield2)
from tab
group by name
drop table tab
------解决方案--------------------
select name,sum(countfield1+countfield2) from [table] group by name
------解决方案--------------------
select name,countfield=sum(countfield1)+sum(countfield2) from A group by name