Link to home
Start Free TrialLog in
Avatar of Luigi Talamo
Luigi Talamo

asked on

query select top n records

Greetings!
in my betting software project i have 2 tables and a query like this:

procedure TMain.Button16Click(Sender: TObject);
begin

ADOTABLE2.first;
while NOT ADOTABLE2.EOF do BEGIN
ADOQuery8.Close;
ADOQuery8.SQL.Clear;
ADOQuery8.SQL.Add ('SELECT SUM(W1) as total1, SUM(B1) as total2, COUNT(*) AS TOT');
ADOQuery8.SQL.Add ('FROM Table1');
ADOQuery8.SQL.Add ('WHERE League = '+QUOTEDSTR(adotable2.FieldByName('League').asstring));
ADOQuery8.Open;

It works very well, but i need to get only the first 30 records from table1 and not the whole records.
The table also has a date field called 'data', i made many tests but with no success...
It is a delphi app (embarcadero xe8) with ado components and ms access tables.

Thanks!
Luigi
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luigi Talamo
Luigi Talamo

ASKER

hi gustav, your tip was fantastic! I only had a problem with the fact that 'top *' select the first n records, but inserting an 'order by data desc' command i got the last n records and it was my goal, thanks alot!!!

ADOQuery8.Close;
ADOQuery8.SQL.Clear;
ADOQuery8.SQL.Add ('SELECT SUM(W1) as totale1, SUM(B1)as totale2, COUNT(*) AS TOTA');
ADOQuery8.SQL.Add ('FROM (Select Top 30 * From Table1 WHERE League = '+QUOTEDSTR(adotable2.FieldByName('League').asstring)+'ORDER BY Data DESC)');
ADOQuery8.Open;
Thanks alot!