数据?
有一表字段a,b,c 如下,我想如果字段a 中两个以上重复记录的时,计算b的和,然后将结果保存到重复记录的第一个的c字段中,例如,将3个114的b字段求和为77,将结果保存到第一个114的c字段中?
a b c
111 55
112 66
113 78
114 12 77
114 13
114 52
115 .
115 .
116 .
117 .
. .
[解决办法]
&&数据库表名为t01
close all
clear
create table t01 (a n(3),b n(3),c n(4))
insert into t01 (a,b) values (111,55)
insert into t01 (a,b) values (112,66)
insert into t01 (a,b) values (113,78)
insert into t01 (a,b) values (114,12)
insert into t01 (a,b) values (114,13)
insert into t01 (a,b) values (114,52)
&&使用temp01为临时表,用毕删除
select a,sum(b) as b from t01 group by a having count(*)> 1 into table temp01
close all
select 1
use t01
list
select 2
use temp01
do while .not. eof()
aa=a
bb=b
select 1
locate for a=aa
replace c with bb
select 2
skip
enddo
select 1
list
&&临时表用毕删除
drop table temp01
close all
[解决办法]
vfp9:
SELECT a.*,nvl(b.cc,0) FROM df a;
left join ;
(SELECT a,MIN(id) as mi,SUM(b) as cc FROM df GROUP BY a HAVING COUNT(*)> =2) b ;
ON a.a=b.a AND a.id=b.mi
以下:
SELECT a,MIN(id) as mi,SUM(b) as cc FROM df GROUP BY a HAVING COUNT(*)> =2 into curs ff nofi
SELECT a.*,nvl(b.cc,0) FROM df a;
left join ff;
ON a.a=b.a AND a.id=b.mi