var sel,sign,sign1,strq,strcd,XSD,XSD1,THD,THD1,HUDC,HUDC1,HUDR,HUDR1,WTJS,WTJS1:string;
begin
sel:='';
sign:='';
sign1:='';
strq:='';
strcd:='';
XSD:='';
XSD1:='';
THD:='';
THD1:='';
HUDC:='';
HUDC1:='';
HUDR:='';
HUDR1:='';
WTJS:='';
WTJS1:='';
//查询语句初始化
//查询列
sel:='SELECT Distinct SPXX_SPBH as 商品编号,SPXX_SPQM as 商品全名,SPXX_XH as 型号,SPXX_GG as 规格'+
',SPXX_CD as 产地,SPXX_DW as 单位,SPXX_FZDW as 辅助单位,SPXX_DWGX as 单位关系,';
//对销售定单的查询组合
XSD:=XSD+'LEFT JOIN (SELECT XSDDMXSPBH,SUM(XSDDMXSL) 商品数量,SUM(XSDDMXJE) 总金额 FROM XSDDMXB';
XSD1:=XSD1+' GROUP BY XSDDMXSPBH) a ON(a.XSDDMXSPBH=SPXX_SPBH)';
//对销售退货的查询组合
THD:=THD+'LEFT JOIN (SELECT XSTHMXSPBH,SUM(XSTHMXSL) 商品数量,SUM(XSTHMXJE) 总金额 FROM XSTHDMXB';
THD1:=THD1+' GROUP BY XSTHMXSPBH) b ON(SPXX_SPBH = b.XSTHMXSPBH)';
//对销售换货换出的组合
HUDC:=HUDC+'LEFT JOIN (SELECT SPBH,SUM(SL) 商品数量,SUM(JE) 总金额 FROM XSHHHCB';
HUDC1:=HUDC1+' GROUP BY SPBH) c ON(SPXX_SPBH = c.SPBH)';
//对销售换货换出的组合
HUDR:=HUDR+'LEFT JOIN (SELECT SPBH,SUM(SL) 商品数量,SUM(JE) 总金额 FROM XSHHHRB';
HUDR1:=HUDR1+' GROUP BY SPBH) d ON(SPXX_SPBH = d.SPBH)';
//对委托结算的组合
WTJS:=WTJS+'LEFT JOIN (SELECT WTJSMXSPBH,SUM(WTJSMXSL) 商品数量,SUM(WTJSMXJE) 总金额 FROM WTJSDMXB';
WTJS1:=WTJS1+' GROUP BY WTJSMXSPBH) e ON(e.WTJSMXSPBH = SPXX_SPBH)';
//销售单查询
if CheckBox1.Checked = true then
begin
strq:=strq + XSD;
strq:=strq + ',XSDDB where XSDDDJBH=XSDDMXDJBH';
if Edit1.Text<>'' then
begin
strq:=strq + ' and XSDDGMDW='''+Edit1.Text+'''';
end;
if Edit2.Text<>'' then
begin
strq:=strq + ' and XSDDJSR='''+Edit2.Text+'''';
end;
if Edit3.Text<>'' then
begin
strq:=strq + ' and XSDDFHCK='''+Edit3.Text+'''';
end;
strq:=strq+' and XSDDLDRQ>='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker1.DateTime)+''''+' and XSDDLDRQ<='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker2.DateTime+1)+''''+XSD1;
end;
//销售退货单
if CheckBox2.Checked = true then
begin
strq:=strq + THD;
strq:=strq + ',XSTHD where XSTHDJBH=XSTHMXDJBH';
if Edit1.Text<>'' then
begin
strq:=strq+' and XSTHTHDW='''+Edit1.Text+'''';
end;
if Edit2.Text<>'' then
begin
strq:=strq+' and XSTHJSR='''+Edit2.Text+'''';
end;
if Edit3.Text<>'' then
begin
strq:=strq+' and XSTHSHCK='''+Edit3.Text+'''';
end;
strq:=strq + ' and XSTHLDSJ>='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker1.DateTime)+''''+' and XSTHLDSJ<='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker2.DateTime+1)+''''+THD1;
end;
//销售换货单
if CheckBox3.Checked = true then
begin
//换货单出条件组合
strq:=strq + HUDC;
strq:=strq + ',XSHHDB where XSHHDB.DJBH=XSHHHCB.DJBH';
if Edit1.Text<>'' then
begin
strq:=strq+' and WLDW='''+Edit1.Text+'''';
end;
if Edit2.Text<>'' then
begin
strq:=strq+' and JSR='''+Edit2.Text+'''';
end;
if Edit3.Text<>'' then
begin
strq:=strq+' and HCCK='''+Edit3.Text+'''';
end;
strq:=strq + ' and LDRQ>='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker1.DateTime)+''''+' and LDRQ<='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker2.DateTime+1)+''''+HUDC1;
//换货单入条件组合
strq:=strq + HUDR;
strq:=strq + ',XSHHDB where XSHHDB.DJBH=XSHHHRB.DJBH';
if Edit1.Text<>'' then
begin
strq:=strq+' and WLDW='''+Edit1.Text+'''';
end;
if Edit2.Text<>'' then
begin
strq:=strq+' and JSR='''+Edit2.Text+'''';
end;
if Edit3.Text<>'' then
begin
strq:=strq+' and HRCK='''+Edit3.Text+'''';
end;
strq:=strq + ' and LDRQ>='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker1.DateTime)+''''+' and LDRQ<='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker2.DateTime+1)+''''+HUDR1;
end;
//委托结算查询
if CheckBox5.Checked = true then
begin
strq:=strq + WTJS;
strq:=strq + ',WTJSDB where WTJSDB.WTJSDJBH=WTJSDMXB.WTJSMXDJBH';
if Edit1.Text<>'' then
begin
strq:=strq+' and WTJSJSDW='''+Edit1.Text+'''';
end;
if Edit2.Text<>'' then
begin
strq:=strq+' and WTJSJSR='''+Edit2.Text+'''';
end;
strq:=strq + ' and WTJSLDRQ>='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker1.DateTime)+''''+' and WTJSLDRQ<='''+SysUtils.formatdatetime('yyyy-mm-dd',DateTimePicker2.DateTime+1)+''''+WTJS1;
end;
//计算公式组合
if CheckBox1.Checked = true then
begin
sign:=sign + 'isnull(a.商品数量,0)';
sign1:=sign1 + 'isnull(a.总金额,0)';
end;
if CheckBox2.Checked = true then
begin
sign:=sign + '-isnull(b.商品数量,0)';
sign1:=sign1 + '-isnull(b.总金额,0)';
end;
if CheckBox3.Checked = true then
begin
sign:=sign + '+isnull(c.商品数量,0)-isnull(d.商品数量,0)';
sign1:=sign1 + '+isnull(c.总金额,0)-isnull(d.总金额,0)';
end;
if CheckBox5.Checked = true then
begin
sign:=sign + '+isnull(e.商品数量,0)';
sign1:=sign1 + '+isnull(e.总金额,0)';
end;
strcd:=strcd + '('+sign+') as 销售数量,case when ('+sign+')<>0 then ('+sign1+')/isnull('+sign+',0) else 0 end 销售均价 '+
'FROM SPXX ';
Query1.ConnectionString := 'DATABASEURL=Biz:\JXC_GJP_\SJK.Database';
if (CheckBox1.Checked = false) and (CheckBox2.Checked = false) and (CheckBox3.Checked = false) and (CheckBox5.Checked = false) then
begin
//在全不选的情况下进行的计算
strcd:= '0 as 销售数量,0 as 销售均价 FROM SPXX';
Query1.CommandText := sel + strcd + strq;
end
else
begin
Query1.CommandText := sel + strcd + strq;
end;
Query1.Active:=true;
Button1.Enabled := false;
这是我写的SQL及代码 |