我用的是一种笨办法。源码如下,请高手说说。
Scondition:='';
Sgroupby:='';
adoquery2.Close;
adoquery2.SQL.clear;
adoquery2.sql.Add ('select ');
if (checkbox2.Checked) then
adoquery2.SQL.Add('a.区名, ');
if (checkbox3.Checked) then
adoquery2.SQL.Add('a.学校, ');
if (checkbox4.Checked) then
adoquery2.SQL.Add('a.年级,');
if (checkbox5.Checked) then
adoquery2.SQL.Add('a.班级, ');
adoquery2.SQL.Add('count(*) as 总人数, ');
adoquery2.sql.Add ('sum(iif(性别='+''''+'女'+''''+',1,0))as 女生人数,');
adoquery2.sql.add('sum(iif(性别='+''''+'男'+''''+',1,0))as 男生人数');
sgroupby:='学籍基本信息表 a,';
if (checkbox2.Checked) then
sgroupby:=sgroupby+'区码表 b';
if (checkbox3.Checked)then
sgroupby:=sgroupby+'校码表 c,';
if (checkbox4.Checked) then
sgroupby:=sgroupby+'年级表 d,';
if (checkbox5.Checked) then
sgroupby:=sgroupby+'班码表 e,';
sgroupby:=copy(sgroupby,1,(strlen(pchar(sgroupby))-1));
adoquery2.SQL.Add(' from '+sgroupby);
sgroupby:='';
if (checkbox2.Checked)then
sgroupby:=sgroupby+' and a.区名=b.区名';
if (checkbox3.Checked)then
sgroupby:=sgroupby+' and a.学校=c.校名';
if (checkbox4.Checked)then
sgroupby:=sgroupby+' and a.年级=d.年级';
if (checkbox5.Checked)then
sgroupby:=sgroupby+' and a.班级=e.班级';
sgroupby:=copy(sgroupby,5,(strlen(pchar(sgroupby))));
if sgroupby<>'' then
adoquery2.SQL.Add(' where '+sgroupby);
sgroupby:='';
if (checkbox2.Checked) then
sgroupby:=sgroupby+'a.区名,b.区码,';
if (checkbox3.Checked) then
sgroupby:=sgroupby+'a.学校,c.校码,';
if (checkbox4.Checked) then
sgroupby:=sgroupby+'a.年级,d.级码,';
if (checkbox5.Checked) then
sgroupby:=sgroupby+'a.班级,e.班码,';
sgroupby:=copy(sgroupby,1,(strlen(pchar(sgroupby))-1));
if sgroupby<>'' then
adoquery2.sql.add('group by '+sgroupby);
sgroupby:='';
if (checkbox2.Checked) then
sgroupby:=sgroupby+'b.区码,';
if (checkbox3.Checked)then
sgroupby:=sgroupby+'c.校码,';
if (checkbox4.Checked) then
sgroupby:=sgroupby+'d.级码,';
if (checkbox5.Checked) then
sgroupby:=sgroupby+'e.班码,';
sgroupby:=copy(sgroupby,1,(strlen(pchar(sgroupby))-1));
if sgroupby<>'' then
adoquery2.sql.Add('order by '+sgroupby) ;
adoquery2.open;
adoquery4.Close;
adoquery4.SQL.Clear ;
adoquery4.SQL.Text :=adoquery2.SQL.GetText ;
adoquery4.Open ;
i:=adoquery4.Fields.Count;
ileft:=10;
if i>0 then
begin
qrlabel1.Caption :=adoquery4.Fields[0].FieldName ;
qrdbtext1.DataField:=adoquery4.Fields[0].FieldName;
qrlabel1.width :=adoquery4.Fields[0].dataSize;
qrdbtext1.width :=adoquery4.fields[0].datasize;
qrlabel1.Left:=ileft;
qrdbtext1.Left:=ileft;
end;
i:=i-1;
if i>0 then
begin
qrlabel2.Caption :=adoquery4.Fields[1].FieldName ;
qrdbtext2.DataField:=adoquery4.Fields[1].FieldName;
qrlabel2.width :=adoquery4.Fields[1].dataSize;
qrdbtext2.width :=adoquery4.fields[1].datasize;
ileft:=ileft+qrlabel1.Width +80;
qrlabel2.Left:=ileft ;
qrdbtext2.Left:=ileft;
end;
i:=i-1;
if i>0 then
begin
qrlabel3.Caption :=adoquery4.Fields[2].FieldName ;
qrdbtext3.DataField:=adoquery4.Fields[2].FieldName;
qrlabel3.width :=adoquery4.Fields[2].dataSize;
qrdbtext3.width :=adoquery4.fields[2].datasize;
ileft:=ileft+qrlabel2.Width +80;
qrlabel3.Left:=ileft ;
qrdbtext3.Left:=ileft;
end;
i:=i-1;
if i>0 then
begin
qrlabel4.Caption :=adoquery4.Fields[3].FieldName ;
qrdbtext4.DataField:=adoquery4.Fields[3].FieldName;
qrlabel4.width :=adoquery4.Fields[3].dataSize;
qrdbtext4.width :=adoquery4.fields[3].datasize;
ileft:=ileft+qrlabel3.Width +80;
qrlabel4.Left:=ileft ;
qrdbtext4.Left:=ileft;
end;
i:=i-1;
if i>0 then
begin
qrlabel5.Caption :=adoquery4.Fields[4].FieldName ;
qrdbtext5.DataField:=adoquery4.Fields[4].FieldName;
qrlabel5.width :=adoquery4.Fields[4].dataSize;
qrdbtext5.width :=adoquery4.fields[4].datasize;
ileft:=ileft+adoquery4.fields[4].datasize +80;
qrlabel5.Left:=ileft ;
qrdbtext5.Left:=ileft;
end;
i:=i-1;
if i>0 then
begin
qrlabel6.Caption :=adoquery4.Fields[5].FieldName ;
qrdbtext6.DataField:=adoquery4.Fields[5].FieldName;
qrlabel6.width :=adoquery4.Fields[5].dataSize;
qrdbtext6.width :=adoquery4.fields[5].datasize;
ileft:=ileft+qrlabel5.Width +80;
qrlabel6.Left:=ileft ;
qrdbtext6.Left:=ileft;
end;
i:=i-1;
if i>0 then
begin
qrlabel7.Caption :=adoquery4.Fields[6].FieldName ;
qrdbtext7.DataField:=adoquery4.Fields[6].FieldName;
qrlabel7.width :=adoquery4.Fields[6].datasize;
qrdbtext7.width :=adoquery4.fields[6].DataSize;
ileft:=ileft+qrlabel6.Width +80;
qrlabel7.Left:=ileft ;
qrdbtext7.Left:=ileft;
end;
quickrep1.Preview ;