34,870
社区成员




create table tb(订单号 int, 物料编号 int, 数量 int)
insert into tb
select 1, 1 , 1 union all
select 2, 2 , 2 union all
select 1, 1 , 3 union all
select 2, 2 , 5 union all
select 3, 1 , 7
go
select 订单号 , 物料编号,SUM(数量) as 数量
from tb
group by 订单号 , 物料编号
/*
订单号 物料编号 数量
1 1 4
3 1 7
2 2 7
*/
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([订单号] int,[物料编号] int,[数量] int)
insert [a]
select 1,1,1 union all
select 2,2,2 union all
select 1,1,3 union all
select 2,2,5 union all
select 3,1,7
select [订单号],[物料编号],sum([数量]) as [数量] into 新表 from a group by [订单号],[物料编号]
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([订单号] int,[物料编号] int,[数量] int)
insert [a]
select 1,1,1 union all
select 2,2,2 union all
select 1,1,3 union all
select 2,2,5 union all
select 3,1,7
select [订单号],[物料编号],sum([数量]) as [数量] from a group by [订单号],[物料编号]
订单号 物料编号 数量
----------- ----------- -----------
1 1 4
3 1 7
2 2 7
(3 行受影响)