我建的表结构和数据如下:(数据库是sql server 2000)CREATE TABLE [T1] ( [Num] [int] NOT NULL , [SL] [int] NULL , CONSTRAINT [PK_T1] PRIMARY KEY CLUSTERED ( [Num] ) ON [PRIMARY] ) ON [PRIMARY]GO Num SL(降水量) 1 2 2 3 3 4 4 10 5 13 6 14 7 15 8 16 9 20 10 21...implementation{$R *.dfm}function GenSQL(const x,y:Integer):string;var sSQL,sColumn:string;
i:integer;
begin
for i:=1 to ydo
begin
sColumn:='['+inttostr((i-1)*x)+'-'+inttostr(i*x)+']';
if i>1 then
sSQL:=sSQL+',';
sSQL:=sSQL+'(select count(*) '+sColumn+' from t1 where sl between '+ inttostr((i-1)*x)+' and '+inttostr(i*x)+') a'+inttostr(i);
end;
result:='select * from '+sSQL;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//edit1.Text='10';
//edit2.Text='3';
ADOQuery1.Active:=false;
ADOQuery1.SQL.Text:=GenSQL(StrToInt(edit1.Text),StrToInt(edit2.Text));
ADOQuery1.Active:=True;
end;