不知道算不算?
strSQL:='select a.zhi as zhi,sum(a.bbyml) as bbyml,avg(a.dwfrl) as dwfrl,';
strSQL:=strSQL+'sum(a.bbyml*a.dwfrl/29.308) as zbml,';
strSQL:=strSQL+'avg(b.lh1_lzkrw) as lh1_lzkrw,avg(b.lh2_lzkrw) as lh2_lzkrw,';
strSQL:=strSQL+'avg(b.lh3_lzkrw) as lh3_lzkrw,';
strSQL:=strSQL+'sum((c.sz-c.jz)*c.pl)/sum(a.bbyml) as fdymhl,';
strSQL:=strSQL+'sum((d.sz-d.jz)*d.pl)/sum(a.bbyml) as gdymhl,';
strSQL:=strSQL+'sum((c.sz-c.jz)*c.pl)/sum(a.bbyml*a.dwfrl/29.308) as fdbmhl,';
strSQL:=strSQL+'sum((d.sz-d.jz)*d.pl)/sum(a.bbyml*a.dwfrl/29.308) as gdbmhl ';
strSQL:=strSQL+' from view_yx_rlmhy as a,view_mzfx_lzjybgb as b,';
strSQL:=strSQL+' dl_data as c,dl_data as d ';
strSQL:=strSQL+'where a.rq=b.rq and b.rq=c.rq and c.rq=d.rq ';
strSQL:=strSQL+' and a.zhi=b.zhi and b.zhi=c.bz and c.bz=d.bz ';
strSQL:=strSQL+' and c.fl='''+'发电量'+''''+' and d.fl='''+'供电量'+'''';
strSQL:=strSQL+' and convert(varchar(10),a.rq,10)>=''';
strSQL:=strSQL+FormatDateTime('yyyy-MM-dd',dtpBeginDate.Date)+'''';
strSQL:=strSQL+' and convert(varchar(10),a.rq,10)<=''';
strSQL:=strSQL+FormatDateTime('yyyy-MM-dd',dtpEndDate.Date)+'''';
strSQL:=strSQL+' group by a.zhi ';
strSQL:=strSQL+' union all ';
strSQL:=strSQL+'select '''+'合计'+''' as zhi,sum(a.bbyml) as bbyml,avg(a.dwfrl) as dwfrl,';
strSQL:=strSQL+'sum(a.bbyml*a.dwfrl/29.308) as zbml,';
strSQL:=strSQL+'avg(b.lh1_lzkrw) as lh1_lzkrw,avg(b.lh2_lzkrw) as lh2_lzkrw,';
strSQL:=strSQL+'avg(b.lh3_lzkrw) as lh3_lzkrw,';
strSQL:=strSQL+'sum((c.sz-c.jz)*c.pl)/sum(a.bbyml) as fdymhl,';
strSQL:=strSQL+'sum((d.sz-d.jz)*d.pl)/sum(a.bbyml) as gdymhl,';
strSQL:=strSQL+'sum((c.sz-c.jz)*c.pl)/sum(a.bbyml*a.dwfrl/29.308) as fdbmhl,';
strSQL:=strSQL+'sum((d.sz-d.jz)*d.pl)/sum(a.bbyml*a.dwfrl/29.308) as gdbmhl ';
strSQL:=strSQL+' from view_yx_rlmhy as a,view_mzfx_lzjybgb as b,';
strSQL:=strSQL+' dl_data as c,dl_data as d ';
strSQL:=strSQL+'where a.rq=b.rq and b.rq=c.rq and c.rq=d.rq ';
strSQL:=strSQL+' and a.zhi=b.zhi and b.zhi=c.bz and c.bz=d.bz ';
strSQL:=strSQL+' and c.fl='''+'发电量'+''''+' and d.fl='''+'供电量'+'''';
strSQL:=strSQL+' and convert(varchar(10),a.rq,10)>=''';
strSQL:=strSQL+FormatDateTime('yyyy-MM-dd',dtpBeginDate.Date)+'''';
strSQL:=strSQL+' and convert(varchar(10),a.rq,10)<=''';
strSQL:=strSQL+FormatDateTime('yyyy-MM-dd',dtpEndDate.Date)+'''';
if(frmDM.cdsTotalMData.Active=true) then
frmDM.cdsTotalMData.Active:=false;
frmDM.cdsTotalMData.CommandText:=strSQL;
frmDM.cdsTotalMData.Active:=true;