看看下面的例子吧!或许对你有所帮助!
procedure TS_Fee_Report.Button_previewClick(Sender: TObject);
var
tempstar1,tempend1,temptime:string;
begin
if S_Mgr_Rpt.RadioBtn_Day.checked then
temptime :='1';
if S_Mgr_Rpt.RadioBtn_Month.checked then
temptime :='30';
if S_Mgr_Rpt.RadioBtn_Select.checked then
temptime :=IntToStr( Trunc ( S_Mgr_Rpt.DateTimePicker_End.Date - S_Mgr_Rpt.DateTimePicker_Start.Date ));
tempstar1 := FormatDateTime('YYYY-MM-DD', S_Mgr_Rpt.DateTimePicker_Start.Date);
tempEnd1 := FormatDateTime('YYYY-MM-DD', S_Mgr_Rpt.DateTimePicker_End.Date);
QRLabel11.caption :=datetimetostr(S_Mgr_Rpt.DateTimePicker_Start.Datetime);
QRLabel12.caption :=datetimetostr(S_Mgr_Rpt.DateTimePicker_end.Datetime);
QuickRep1.ReportTitle :=s_mgr_rpt.Edit_Title.Text;
if S_Mgr_Rpt.ListBox_Order.Itemindex <> -1 then
case S_Mgr_Rpt.ListBox_Order.ItemIndex of
0 : begin
with query1 do begin
close;
sql.clear;
databasename := 'db';
sql.Add('SELECT GDR0502, GDD0202, SUM(GDR0503), (GDR0204*'''+temptime+'''/(GDR0203-GDR0202 + 1)) fee,SUM(GDR0503)/(GDR0204*30/(GDR0203-GDR0202 + 1))');
sql.Add('FROM ge_jc.GDR05 , ge_jc.GDR02 , ge_jc.GDD02 ');
sql.Add('where');
sql.Add(' GDR0201 = GDR0502');
sql.Add('and');
sql.Add(' GDD0201 = GDR0502');
sql.Add('and');
sql.Add('GDR0504 >= to_date(''' + tempstar1 + ''',''YYYY-MM-DD'')');
SQL.Add('and');
sql.Add('GDR0504 <= to_date(''' + tempend1 + ''',''YYYY-MM-DD'')');
sql.Add('group by');
sql.Add(' GDR0502,(GDR0203-GDR0202 + 1), GDR0204, GDD0202');
sql.add('order by');
sql.Add('sum(GDR0503)');
query1.open;
end;
if query1.RecordCount <1 then
begin
application.createform(ts_NULL,S_NULL);
S_NULL.SHOWMODAL;
end
else
QuickRep1.preview;
end;
1 : begin
with query1 do begin
close;
sql.clear;
databasename := 'db';
sql.Add('SELECT GDR0502, GDD0202, SUM(GDR0503), (GDR0204*'''+temptime+'''/(GDR0203-GDR0202 + 1)) ,SUM(GDR0503)/(GDR0204*30/(GDR0203-GDR0202 + 1))');
sql.Add('FROM GDR05 , GDR02 , GDD02 ');
sql.Add('where');
sql.Add(' GDR0201 = GDR0502');
sql.Add('and');
sql.Add(' GDD0201 = GDR0502');
sql.Add('and');
sql.Add('GDR0504 >= to_date(''' + tempstar1 + ''',''YYYY-MM-DD'')');
SQL.Add('and');
sql.Add('GDR0504 <= to_date(''' + tempend1 + ''',''YYYY-MM-DD'')');
sql.Add('group by');
sql.Add(' GDR0502,(GDR0203-GDR0202 + 1), GDR0204, GDD0202');
sql.add('order by');
sql.Add('(GDR0204*30/(GDR0203-GDR0202 + 1))');
query1.open;
end;
if query1.RecordCount <1 then
begin
application.createform(ts_NULL,S_NULL);
S_NULL.SHOWMODAL;
end
else
QuickRep1.preview;
end;
2 : begin
with query1 do begin
close;
sql.clear;
databasename := 'db';
sql.Add('SELECT GDR0502, GDD0202, SUM(GDR0503), (GDR0204*'''+temptime+'''/(GDR0203-GDR0202 + 1)) ,SUM(GDR0503)/(GDR0204*30/(GDR0203-GDR0202 + 1))');
sql.Add('FROM GDR05 , GDR02 , GDD02 ');
sql.Add('where');
sql.Add(' GDR0201 = GDR0502');
sql.Add('and');
sql.Add(' GDD0201 = GDR0502');
sql.Add('and');
sql.Add('GDR0504 >= to_date(''' + tempstar1 + ''',''YYYY-MM-DD'')');
SQL.Add('and');
sql.Add('GDR0504 <= to_date(''' + tempend1 + ''',''YYYY-MM-DD'')');
sql.Add('group by');
sql.Add(' GDR0502,(GDR0203-GDR0202 + 1), GDR0204, GDD0202');
sql.add('order by');
sql.Add('SUM(GDR0503)/(GDR0204*30/(GDR0203-GDR0202 + 1))');
query1.open;
end;
if query1.RecordCount <1 then
begin
application.createform(ts_NULL,S_NULL);
S_NULL.SHOWMODAL;
end
else
QuickRep1.preview;
end;
end//of case
else
begin
with query1 do begin
close;
sql.clear;
databasename := 'db';
sql.Add('SELECT GDR0502, GDD0202, SUM(GDR0503), (GDR0204*'''+temptime+'''/(GDR0203-GDR0202 + 1)) fee,SUM(GDR0503)/(GDR0204*30/(GDR0203-GDR0202 + 1))');
sql.Add('FROM ge_jc.GDR05 , ge_jc.GDR02 , ge_jc.GDD02 ');
sql.Add('where');
sql.Add(' GDR0201 = GDR0502');
sql.Add('and');
sql.Add(' GDD0201 = GDR0502');
sql.Add('and');
sql.Add('GDR0504 >= to_date(''' + tempstar1 + ''',''YYYY-MM-DD'')');
SQL.Add('and');
sql.Add('GDR0504 <= to_date(''' + tempend1 + ''',''YYYY-MM-DD'')');
sql.Add('group by');
sql.Add(' GDR0502,(GDR0203-GDR0202 + 1), GDR0204, GDD0202');
sql.add('order by');
sql.Add('sum(GDR0503)');
query1.open;
end;
if query1.RecordCount <1 then
begin
application.createform(ts_NULL,S_NULL);
S_NULL.SHOWMODAL;
end
else
QuickRep1.preview;
end;
end;
注:用数据库的QUERY控件把DBGRID的列用SQL语句选出来,然后设置他的一些相关属性就OK了