问题描述:一张表中有两个字段,一个是名称,一个是金额,假如名称相同,则合并,金额相加。
解法:
1、先建表
[sql]create table tablet ( name VARCHAR2(40) not null, price NUMBER(4) not null ) create table tablew ( name VARCHAR2(40) not null, price NUMBER(4) not null )[/sql]
2、建立两个数据集cds1、cds2,cds1记录原始数据,cds2记录合并后的数据:
[delphi] begin cds1.Close; cds1.CommandText := ' select name, price from tablet '; cds1.Open; cds2.Close; cds2.CommandText := ' select name, price from tablew where 1=0 '; cds2.Open; cds1.First; while not cds1.Eof do begin cds2.First; if cds2.Locate('name',cds1.FieldByName('name').AsString,[] ) = false then begin cds2.Append; cds2.FieldByName('name').AsString := cds1.FieldByName('name').AsString; cds2.FieldByName('price').AsInteger := cds1.FieldByName('price').AsInteger; cds2.Post; end else begin cds2.Edit; cds2.FieldByName('price').AsInteger := cds2.FieldByName('price').AsInteger + cds1.FieldByName('price').AsInteger; cds2.Post; end; cds1.Next; end; end; [/delphi]