L
liang1
Unregistered / Unconfirmed
GUEST, unregistred user!
各位老师: 请教一个问题,一个查询工资的表,数据不多但查询的数据也很慢(注:结果中有好几个计算字段),也没有用存储过程等,请问该如何处理?代码如下:procedure TGongZiBiaoFm.RzBitBtn3Click(Sender: TObject);begin screen.Cursor:=crSQLWait; n1:=FormatDateTime('yyyy',RzDateTimePicker1.DateTime); y1:=IntToStr(strToint(FormatDateTime('mm',RzDateTimePicker1.DateTime))); d1:=FormatDateTime('yyyy',RzDateTimePicker1.DateTime)+'-'+FormatDateTime('mm',RzDateTimePicker1.DateTime)+'-1'; if y1='12' then d2:=IntToStr(strToint(FormatDateTime('yyyy',RzDateTimePicker1.DateTime))+1)+'-1-1' else d2:=FormatDateTime('yyyy',RzDateTimePicker1.DateTime)+'-'+IntToStr(strToint(FormatDateTime('mm',RzDateTimePicker1.DateTime))+1)+'-1'; if Trim(e3.Text)<>'所有人员' then Begin ADOQuery1.Close; ADOQuery1.SQL.Clear; // ADOQuery1.SQL.Add('Select * from ygzl where lz=0 and zm like ''%'+Trim(flatEdit1.Text)+'%'''); ADOQuery1.SQL.Add('select distinct uName from zy_gzb where nian='''+n1+''''+' and yue='''+y1+''''+' and uName in (select uName from ygzl where zm='''+Trim(e3.Text)+''''+')'); ADOQuery1.Open; end else Begin ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select distinct uName from zy_gzb where nian='''+n1+''''+' and yue='''+y1+''''); ADOQuery1.Open; end; screen.Cursor:=crDefault;end;procedure TGongZiBiaoFm.ADOQuery1CalcFields(DataSet: TDataSet); var sql1,sql2:string;begin sql1:='select sum(r1+r2+r3+r4+r5+r6+r7+r8+r9+r10+r11+r12+r13+r14+r15+r16+r17+r18+r19+r20+r21+r22+r23+r24+r25+r26+r27+r28+r29+r30+r31) as yf_jj from zy_gzb where nian='''+n1+''''+' and yue='''+y1+''''+' and jsfs='''+'计件'+''''+' and uName='''+Trim(ADOQuery1.FieldByname('uName').AsString)+''''; sql2:='select sum(r1+r2+r3+r4+r5+r6+r7+r8+r9+r10+r11+r12+r13+r14+r15+r16+r17+r18+r19+r20+r21+r22+r23+r24+r25+r26+r27+r28+r29+r30+r31) as yf_js from zy_gzb where nian='''+n1+''''+' and yue='''+y1+''''+' and jsfs='''+'计时'+''''+' and uName='''+Trim(ADOQuery1.FieldByname('uName').AsString)+''''; ADOQuery2.Close; ADOQuery2.SQL.Clear; ADOQuery2.SQL.Add(sql1); //showmessage(adoquery2.SQL.GetText); ADOQuery2.Open; if not ADOQuery2.Eof then ADOQuery1.FieldByName('yf_jj').AsFloat:=ADOQuery2.FieldbyName('yf_jj').AsFloat else ADOQuery1.FieldByName('yf_jj').AsFloat:=0; // ADOQuery2.Close; ADOQuery2.SQL.Clear; ADOQuery2.SQL.Add('select bank from ygzl where uName='''+ADOQuery1.FieldByName('uName').AsString+''''); //showmessage(adoquery2.SQL.GetText); ADOQuery2.Open; if not ADOQuery2.Eof then ADOQuery1.FieldByName('bank').asString:=ADOQuery2.FieldbyName('bank').AsString else ADOQuery1.FieldByName('bank').AsString:='';// ADOQuery2.Close; ADOQuery2.SQL.Clear; ADOQuery2.SQL.Add(sql2); ADOQuery2.Open; if not ADOQuery2.Eof then ADOQuery1.FieldByName('yf_js').AsFloat:=ADOQuery2.FieldbyName('yf_js').AsFloat else ADOQuery1.FieldByName('yf_js').AsFloat:=0; ADOQuery2.Close; ADOQuery2.SQL.Clear; ADOQuery2.SQL.Add('select sum(jj) as jj,sum(bt) as bt,sum(qtf1+qtf2) as yf_qt from gz_jia where uName='''+ADOQuery1.FieldByName('uName').Asstring+''''+' and rq>='''+d1+''''+' and rq<'''+d2+''''); ADOQuery2.Open; if not ADOQuery2.Eof then Begin ADOQuery1.FieldByName('yf_bt').AsFloat:=ADOQuery2.FieldbyName('bt').AsFloat; ADOQuery1.FieldByName('yf_jiangjin').AsFloat:=ADOQuery2.FieldbyName('jj').AsFloat; ADOQuery1.FieldByName('yf_qt').AsFloat:=ADOQuery2.FieldbyName('yf_qt').AsFloat; end else Begin ADOQuery1.FieldByName('yf_bt').AsFloat:=0; ADOQuery1.FieldByName('yf_jiangjin').AsFloat:=0; ADOQuery1.FieldByName('yf_qt').AsFloat:=0; end; ADOQuery1.FieldByName('yf_hj').AsFloat:=ADOQuery1.FieldByName('yf_jj').AsFloat+ADOQuery1.FieldByName('yf_js').AsFloat+ADOQuery1.FieldByName('yf_bt').AsFloat+ADOQuery1.FieldByName('yf_jiangjin').AsFloat+ADOQuery1.FieldByName('yf_qt').AsFloat;//计算应扣部分 ADOQuery2.Close; ADOQuery2.SQL.Clear; ADOQuery2.SQL.Add('select sum(cf) as cf,sum(jk) as jk,sum(zsf) as zxf,sum(sbf) as sbf,sum(ybf) as ybf,sum(qtf1+qtf2) as yk_qt from gz_ke where uName='''+ADOQuery1.FieldByName('uName').Asstring+''''+' and rq>='''+d1+''''+' and rq<'''+d2+''''); ADOQuery2.Open; if not ADOQuery2.Eof then Begin ADOQuery1.FieldByName('yk_cf').AsFloat:=ADOQuery2.FieldbyName('cf').AsFloat; ADOQuery1.FieldByName('yk_zxf').AsFloat:=ADOQuery2.FieldbyName('zxf').AsFloat; ADOQuery1.FieldByName('yk_jk').AsFloat:=ADOQuery2.FieldbyName('jk').AsFloat; ADOQuery1.FieldByName('yk_ybf').AsFloat:=ADOQuery2.FieldbyName('ybf').AsFloat; ADOQuery1.FieldByName('yk_sbf').AsFloat:=ADOQuery2.FieldbyName('sbf').AsFloat; ADOQuery1.FieldByName('yk_qt').AsFloat:=ADOQuery2.FieldbyName('yk_qt').AsFloat; end else Begin ADOQuery1.FieldByName('yk_cf').AsFloat:=0; ADOQuery1.FieldByName('yk_zxf').AsFloat:=0; ADOQuery1.FieldByName('yk_jk').AsFloat:=0; ADOQuery1.FieldByName('yk_ybf').AsFloat:=0; ADOQuery1.FieldByName('yk_sbf').AsFloat:=0; ADOQuery1.FieldByName('yk_qt').AsFloat:=0; end; ADOQuery1.FieldByName('yk_hj').AsFloat:=ADOQuery1.FieldByName('yk_cf').AsFloat+ADOQuery1.FieldByName('yk_zxf').AsFloat+ADOQuery1.FieldByName('yk_jk').AsFloat+ADOQuery1.FieldByName('yk_ybf').AsFloat+ADOQuery1.FieldByName('yk_sbf').AsFloat+ADOQuery1.FieldByName('yk_qt').AsFloat; ADOQuery1.FieldByName('sf').AsFloat:=ADOQuery1.FieldByName('yf_hj').AsFloat-ADOQuery1.FieldByName('yk_hj').AsFloat;end;