对adoquery1的float型字段a求和,除以adoquery1记录数,结果复制给adoquery2的float型字段b,代码怎么写?(50分)

  • 主题发起人 主题发起人 yyss
  • 开始时间 开始时间
Y

yyss

Unregistered / Unconfirmed
GUEST, unregistred user!
对adoquery1的float型字段a求和,除以adoquery1记录数,结果复制给adoquery2的float型字段b,代码怎么写?
 
var<br>&nbsp; Sum:Double;<br>&nbsp; RecMark,Count:Integer;<br>begin<br>&nbsp; Sum:=0;<br>&nbsp; with adoquery1 do<br>&nbsp; begin<br>&nbsp; &nbsp; Count:=RecordCount;<br>&nbsp; &nbsp; RecMark:=RecNo;<br>&nbsp; &nbsp; DiableControls;<br>&nbsp; &nbsp; First;<br>&nbsp; &nbsp; while not Eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Sum:=Sum+FieldByName('a').AsFloat;<br>&nbsp; &nbsp; &nbsp; Next;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; RecNo:=RecMark;<br>&nbsp; &nbsp; EnableControls;<br>&nbsp; end;<br>&nbsp; if Count&gt;0 then<br>&nbsp; &nbsp; adoquery2.FieldByName('b').AsFloat:=Sum/Count;<br>end;
 
select a,avg(a) as b from table1
 
Sum是Double型,Count是Integer型,能运算吗?<br>我这样不可以:<br>adoquery2.FieldByName('bzzs').AsFloat:=sum(FieldByName('ljzs')/adoquery1.RecordCount;
 
adoquery1.fieldbyname ('b').asfloat:=<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;adoquery1.fieldbyname ('a').asfloat/adoquery1.recordcount<br>后边adoquery1.recordcount是INT类型的,将自动转换,不会有什么问题的.还有要保证数据以经SELECT出来了.
 
adoquery2.FieldByName('bzzs').AsFloat:=sum(FieldByName('ljzs').AsFloat)/adoquery1.RecordCount;<br>提示需要变量,sum(FieldByName('ljzs').AsFloat)有错误,怎么写
 
delphi里那里有sum函数,我服了你,补充下<br>var<br>&nbsp; Sum:Double;<br>&nbsp; RecMark,Count:Integer;<br>begin<br>&nbsp; Sum:=0;<br>&nbsp; with adoquery1 do<br>&nbsp; begin<br>&nbsp; &nbsp; Count:=RecordCount;<br>&nbsp; &nbsp; RecMark:=RecNo;<br>&nbsp; &nbsp; DiableControls;<br>&nbsp; &nbsp; First;<br>&nbsp; &nbsp; while not Eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Sum:=Sum+FieldByName('a').AsFloat;<br>&nbsp; &nbsp; &nbsp; Next;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; RecNo:=RecMark;<br>&nbsp; &nbsp; EnableControls;<br>&nbsp; end;<br>&nbsp; if Count&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; adoquery2.Edit; &nbsp;//注意必须先定位到你要的记录,还有数据集必须打开<br>&nbsp; &nbsp; adoquery2.FieldByName('b').AsFloat:=Sum/Count;<br>&nbsp; &nbsp; adoquery2.Post;<br>&nbsp; end; &nbsp;<br>end;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部