select name,sum(num1)num1,sum(num2)num2 from tb group by name
/* 原表 NAME NUM1 NUM2 张三 5 11 李四 6 5 张三 10 10 李四 20 5 目标 NAME NUM1 NUM2 张三 15 21 李四 26 10 */ go if OBJECT_ID('tbl')is not null drop table tbl go create table tbl( NAME varchar(10), NUM1 int, NUM2 int ) go insert tbl select '张三',5,11 union all select '李四',6,5 union all select '张三',10,10 union all select '李四',20,5select NAME, SUM(NUM1) as NUM1, SUM(NUM2) as NUM2 from tbl group by NAME/* NAME NUM1 NUM2 李四 26 10 张三 15 21 */
select NAME,SUM(NUM1) NUM1,SUM(NUM2) NUM2 from tb group by NAME
from tb
group by name
/*
原表
NAME NUM1 NUM2
张三 5 11
李四 6 5
张三 10 10
李四 20 5
目标
NAME NUM1 NUM2
张三 15 21
李四 26 10
*/
go
if OBJECT_ID('tbl')is not null
drop table tbl
go
create table tbl(
NAME varchar(10),
NUM1 int,
NUM2 int
)
go
insert tbl
select '张三',5,11 union all
select '李四',6,5 union all
select '张三',10,10 union all
select '李四',20,5select NAME,
SUM(NUM1) as NUM1,
SUM(NUM2) as NUM2
from tbl
group by NAME/*
NAME NUM1 NUM2
李四 26 10
张三 15 21
*/
group by NAME