有个表TABLE1,有四个字段TID(唯一ID),POID(采购单号),LN(采购单行号),JINE(金额)
要把相同采购订单号(也就是POID相同)的记录中,金额正负相加未零的记录删除,与LN行号无关。
比如:
POID LN JINE
p001 1 2000
p001 2 300
p001 3 -2000
p001 4 500
p001 5 -300
如何才能得到这样的结果:
POID LN JINE
p001 4 500我的朋友说要用到游标,可惜我没学过,郁闷,不知改怎么写SQL语句才能得到上面的结果?(我用VB写了个程序,先把JINE为正数的放到一个表,再把JINE为负数的放到一个表,然后从正数表中每一条记录查找对应负数表中金额绝对值相等的那条,如果找到,记下TID,然后在TABLE1中把它删除。
不过这样处理感觉好慢,一秒中才能处理10条左右的记录。)
要把相同采购订单号(也就是POID相同)的记录中,金额正负相加未零的记录删除,与LN行号无关。
比如:
POID LN JINE
p001 1 2000
p001 2 300
p001 3 -2000
p001 4 500
p001 5 -300
如何才能得到这样的结果:
POID LN JINE
p001 4 500我的朋友说要用到游标,可惜我没学过,郁闷,不知改怎么写SQL语句才能得到上面的结果?(我用VB写了个程序,先把JINE为正数的放到一个表,再把JINE为负数的放到一个表,然后从正数表中每一条记录查找对应负数表中金额绝对值相等的那条,如果找到,记下TID,然后在TABLE1中把它删除。
不过这样处理感觉好慢,一秒中才能处理10条左右的记录。)
POID LN JINE
p001 1 2000
p001 3 -2000
p001 5 -2000
如何才能得到这样的结果:
POID LN JINE
p001 5 -2000
或者这样的结果也对:
POID LN JINE
p001 3 -2000
反正正负相抵的记录删除就行,行号不管他
from table1
group by POID
insert into tb
select 'p001',1,2000 union all
select 'p001',2,300 union all
select 'p001',3,-2000 union all
select 'p001',4,500 union all
select 'p001',5,-300
go
delete a
from tb a where
exists (
select 1 from tb where poid=a.poid and jine=-a.jine
)
select * from tb
drop table tb
insert @t
select 'p001',1, 2000 union all
select 'p001',2, 300 union all
select 'p001',3, -2000 union all
select 'p001',4, 500 union all
select 'p001',5, -300
select t.* from @t t where jine=(select sum(jine) from @t where poid=t.poid)
但是我按照您的算法,求出 的YUE之和和原先表的YUE之和数据相差4万,应该从哪里找原因呢?
25609 31000900 -666.84
25615 31000900 -666.84
25617 31000900 666.84
25619 31000900 666.84
25621 31000900 666.84
25614 31000900 -939.96
25620 31000900 939.96
25618 31000900 939.96
25613 31000900 -1606.79
25622 31000900 1606.79
24634 31000900 -3034.7
24635 31000900 3034.7
原因找到了,如果是上面这些记录,如果按照wgsasd311(自强不息) 的SQL语句,这些记录就全部被删除了,但实际是有些不用删除的。
insert into @t select 25609,'31000900',-666.84
insert into @t select 25615,'31000900',-666.84
insert into @t select 25617,'31000900',666.84
insert into @t select 25619,'31000900',666.84
insert into @t select 25621,'31000900',666.84
insert into @t select 25614,'31000900',-939.96
insert into @t select 25620,'31000900',939.96
insert into @t select 25618,'31000900',939.96
insert into @t select 25613,'31000900',-1606.79
insert into @t select 25622,'31000900',1606.79
insert into @t select 24634,'31000900',-3034.7
insert into @t select 24635,'31000900',3034.7delete t
from
@t t,
(select a.*,Num=(select count(*) from @t where POID=a.POID and JINE=a.JINE and TID<=a.TID) from @t a) c,
(select a.*,Num=(select count(*) from @t where POID=a.POID and JINE=a.JINE and TID<=a.TID) from @t a) d
where
t.TID=c.TID and c.POID=d.POID and c.JINE=-d.JINE and c.Num=d.Numselect * from @t
create table tb(poid varchar(10),ln int,jine money)
insert into tb
select 'p001',1,2000 union all
select 'p001',2,300 union all
select 'p001',3,-2000 union all
select 'p001',4,500 union all
select 'p001',5,-300 union all
select 'p001',6,-2000
godelete b from tb a,tb b where (
select sum(jine) from tb where poid=a.poid and ln<=a.ln and abs(jine)=abs(a.jine)
)=0 and a.poid=b.poid and abs(a.jine)=abs(b.jine) and a.ln>=b.ln
select * from tb
drop table tb