有一个表其中有列名bill,其中有3个数据,分别是001,002,003还有列名分别 cntr ,article ,qty (表如下:)bill cntr article qty
001 2001 2005 1.00
002 2002 2005 2.00
003 2003 2005 1.00
001 2002 2005 1.00
003 2001 2005 1.00
. . . .
. . . .
. . . .
数据有很多
其中cntr+article 是物品的唯一码,qty这列是数量,要实现cntr+article 有重复的把数量(qty)加到bill=001中,然后删掉cntr+article 在列bill=001之外的那两条(即在bill=002,bill=003中的)。
(注:在每个bill中不会有重复的cntr+article 出现)
001 2001 2005 1.00
002 2002 2005 2.00
003 2003 2005 1.00
001 2002 2005 1.00
003 2001 2005 1.00
. . . .
. . . .
. . . .
数据有很多
其中cntr+article 是物品的唯一码,qty这列是数量,要实现cntr+article 有重复的把数量(qty)加到bill=001中,然后删掉cntr+article 在列bill=001之外的那两条(即在bill=002,bill=003中的)。
(注:在每个bill中不会有重复的cntr+article 出现)
update t
set
qty=(select sum(qty) from 表 where cntr=t.cntr and article=t.article)
from
表 t
where
bill='001'--删除bill不为'001'的记录
delete 表 where bill!='001'
insert @t
select '001', '2001', '2005', 1.00 union all
select '002', '2002', '2005', 2.00 union all
select '003', '2003', '2005', 1.00 union all
select '001', '2002', '2005', 1.00 union all
select '003', '2001', '2005', 1.00select min(bill),(cntr+article),sum(qty) from @t
group by (cntr+article)--结果
---- -------- ----------------------------------------
-- 001 20012005 2.00
-- 001 20022005 3.00
-- 003 20032005 1.00
--
-- (所影响的行数为 3 行)--把得到的结果插入临时表,删除这个表的所有数据,再把临时表的数据导入
insert @t
select '001','2001','2005',1.00 union all
select '001','2001','2005',2.00 union all
select '002','2002','2005',3.00 union all
select '002','2002','2005',2.00 union all
select '003','2003','2005',1.00 union all
select '001','2002','2005',1.00 union all
select '003','2001','2005',1.00 union all
select '003','2001','2005',2.00 union all
select '002','2002','2005',3.00 select bill=min(bill),cntr,article,qty=sum(qty)
from @t
group by bill,cntr,article
----结果
001 2001 2005 3.00
001 2002 2005 1.00
002 2002 2005 8.00
003 2001 2005 3.00
003 2003 2005 1.00