这好说,动态去组织它吗。
看看我的程序吧!
procedure TRe_KCPD_frm.BitBtn1Click(Sender: TObject);
var
m,n:Integer;
sql,s_item,s_price,dat1,dat2,s_type:wideString;
begin
s_type:='';
if sbType.Down then
begin
if cbType.Text='西药片剂' then s_type:=' and b.typename=''西药片剂'' ';
if cbType.Text='针粉酊膏' then s_type:=' and b.typename in(''西药针剂'',''器械'',''非药品'') ';
if cbType.Text='中成药' then s_type:=' and b.typename=''中药'' ';
end;
if not(sbSPS.Down or sbQCKC.Down or sbWFS.Down or sbZMS.Down or sbJHS.Down or sbXSS.Down) then
begin
showmessage('没有选查询项');
Exit;
end;
if sbStarDate.Down then
dat1:=DateTimeToStr(dtpStarDate.Datetime)
else
dat1:='1980-01-01';
if sbLastDate.Down then
dat2:=DateTimeToStr(dtpLastDate.Datetime)
else
dat2:='2100-01-01';
s_price:='';
if sbMinWsprice.Down then
s_price:=s_price+' and a.wsprice>='+FloatToStr(seMinWsprice.Value);
if sbMaxWspirce.Down then
s_price:=s_price+' and a.wsprice<='+FloatToStr(seMaxWsprice.Value);
if sbMinRprice.Down then
s_price:=s_price+' and a.rprice>='+FloatToStr(seMinRprice.Value);
if sbMaxRprice.Down then
s_price:=s_price+' and a.rprice<='+FloatToStr(seMaxRprice.Value);
m:=lvItemList.Items.Count;
if sbItemList.Down and (m>0) then
begin
s_item:=' and a.itemdcode in (';
for n:=1 to m do
if m=n then
s_item:=s_item+''''+lvItemList.Items[n-1].Caption+''''+') '
else
s_item:=s_item+''''+lvItemList.Items[n-1].Caption+''''+',';
end;
sql:=' Select itemdcode,itemname,speci,qtyunit,proname,wsprice'; //,SUM(a1) AS SPXL, SUM(a2) AS QCKK, SUM(a1)-SUM(a3) AS ZMS, SUM(a3) AS WFS, SUM(a4) AS WFJE, SUM(a5) AS JH, SUM(a6) AS XS ';
if sbSPS.Down then sql:=sql+' , SUM(a1) AS SPSL, (SUM(a1)*wsprice) AS SPJE ';
if sbZMS.Down then sql:=sql+' , (SUM(a1)-SUM(a3)) AS ZMSL, (SUM(a1)-SUM(a3))*wsprice AS ZMJE ';
if sbWFS.Down then sql:=sql+' , SUM(a3) AS WFSL, SUM(a4) AS WFJE ';
if sbQCKC.Down then sql:=sql+' , SUM(a2) AS QCKK ';
if sbJHS.Down then sql:=sql+' , SUM(a5) AS JH ';
if sbXSS.Down then sql:=sql+' , SUM(a6) AS XS ';
sql:=sql+' from ( ';
if sbSPS.Down then
begin
sql:=sql+' select a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice';//,SUM(a.qty) AS a1,0 AS a2,0 AS a3,0 AS a4,0 AS a5,0 AS a6 '
if sbSPS.Down then sql:=sql+' , SUM(a.qty) AS a1 ';
if sbQCKC.Down then sql:=sql+' , 0 AS a2 ';
if sbWFS.Down or sbZMS.Down then sql:=sql+' , 0 AS a3, 0 AS a4 ';
if sbJHS.Down then sql:=sql+' , 0 AS a5 ';
if sbXSS.Down then sql:=sql+' , 0 AS a6 ';
sql:=sql+' from t_itembase AS c INNER JOIN(t_itembusiness AS b INNER JOIN t_salelist AS a ON b.itemdcode=a.itemdcode) ON c.itemdcode=a.itemdcode '
+' where whflag>=''2'' and saleflag>=''2'' '+s_price+s_item+s_type+' and a.configdate= '
+' (select distinct max(configdate) from t_salelist where itemdcode=a.itemdcode and whflag>=''2'' and saleflag>=''2'' and configdate<=#'+dat2+'#) '
+' group by a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';
if sbQCKC.Down or sbWFS.Down or sbZMS.Down or sbJHS.Down or sbXSS.Down then sql:=sql+' union '
end;
if sbQCKC.Down then
begin
sql:=sql+' select a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';//,0 AS a1,SUM(a.qty) AS a2,0 AS a3,0 AS a4,0 AS a5,0 AS a6 '
if sbSPS.Down then sql:=sql+' , 0 AS a1 ';
if sbQCKC.Down then sql:=sql+' , SUM(a.qty) AS a2 ';
if sbWFS.Down or sbZMS.Down then sql:=sql+' , 0 AS a3, 0 AS a4 ';
if sbJHS.Down then sql:=sql+' , 0 AS a5 ';
if sbXSS.Down then sql:=sql+' , 0 AS a6 ';
sql:=sql+' from t_itembase AS c INNER JOIN(t_itembusiness AS b INNER JOIN t_salelist AS a ON b.itemdcode=a.itemdcode) ON c.itemdcode=a.itemdcode '
+' where whflag>=''2'' and saleflag>=''2'' '+s_price+s_item+s_type+' and a.configdate= '
+' (select distinct max(configdate) from t_salelist where itemdcode=a.itemdcode and whflag>=''2'' and saleflag>=''2'' and configdate<=#'+dat1+'#) '
+' group by a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';
if sbWFS.Down or sbZMS.Down or sbJHS.Down or sbXSS.Down then sql:=sql+' union '
end;
if sbZMS.Down or sbWFS.Down then
begin
sql:=sql+' select a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';//,0 AS a1,0 AS a2,SUM(a.qtypaid) AS a3,SUM(amtpaid) AS a4,0 AS a5,0 AS a6 '
if sbSPS.Down then sql:=sql+' , 0 AS a1 ';
if sbQCKC.Down then sql:=sql+' , 0 AS a2 ';
if sbWFS.Down or sbZMS.Down then sql:=sql+' , SUM(a.qtypaid) AS a3, SUM(amtpaid) AS a4 ';
if sbJHS.Down then sql:=sql+' , 0 AS a5 ';
if sbXSS.Down then sql:=sql+' , 0 AS a6 ';
sql:=sql+' from t_itembase AS c INNER JOIN(t_itembusiness AS b INNER JOIN t_salelist AS a ON b.itemdcode=a.itemdcode) ON c.itemdcode=a.itemdcode '
+' where optype=''购入'' and whflag>=''2'' and saleflag>=''2'' '+s_price+s_item+s_type+' and configdate>=#'+dat1+'# and configdate<=#'+dat2+'# '
+' group by a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';
if sbJHS.Down or sbXSS.Down then sql:=sql+' union '
end;
if sbJHS.Down then
begin
sql:=sql+' select a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';//,0 AS a1,0 AS a2,0 AS a3,0 AS a4,SUM(a.qty) AS a5,0 AS a6 '
if sbSPS.Down then sql:=sql+' , 0 AS a1 ';
if sbQCKC.Down then sql:=sql+' , 0 AS a2 ';
if sbWFS.Down or sbZMS.Down then sql:=sql+' , 0 AS a3, 0 AS a4 ';
if sbJHS.Down then sql:=sql+' , SUM(a.qty) AS a5 ';
if sbXSS.Down then sql:=sql+' , 0 AS a6 ';
sql:=sql+' from t_itembase AS c INNER JOIN(t_itembusiness AS b INNER JOIN t_salelist AS a ON b.itemdcode=a.itemdcode) ON c.itemdcode=a.itemdcode '
+' where optype=''购入'' and whflag>=''2'' and saleflag>=''2'' '+s_price+s_item+s_type+' and configdate>=#'+dat1+'# and configdate<=#'+dat2+'# '
+' group by a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';
if sbXSS.Down then sql:=sql+' union ';
end;
if sbXSS.Down then
begin
sql:=sql+' select a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';//,0 AS a1,0 AS a2,0 AS a3,0 AS a4,0 AS a5,SUM(a.qty) AS a6 '
if sbSPS.Down then sql:=sql+' , 0 AS a1 ';
if sbQCKC.Down then sql:=sql+' , 0 AS a2 ';
if sbWFS.Down or sbZMS.Down then sql:=sql+' , 0 AS a3, 0 AS a4 ';
if sbJHS.Down then sql:=sql+' , 0 AS a5 ';
if sbXSS.Down then sql:=sql+' , SUM(a.qty) AS a6 ';
sql:=sql+' from t_itembase AS c INNER JOIN(t_itembusiness AS b INNER JOIN t_salelist AS a ON b.itemdcode=a.itemdcode) ON c.itemdcode=a.itemdcode '
+' where optype=''批发'' and whflag>=''2'' and saleflag>=''2'' '+s_price+s_item+s_type+' and configdate>=#'+dat1+'# and configdate<=#'+dat2+'# '
+' group by a.itemdcode,b.itemname,b.speci,b.qtyunit,c.proname,b.wsprice ';
end;
sql:=sql+' ) '
+' Group by itemdcode,itemname,speci,qtyunit,proname,wsprice ';
if cdsKCPD.Active then cdsKCPD.Close;
cdsKCPD.DataRequest(sql);
cdsKCPD.Open;
pnlKclFind.Visible:=False;
end;