Display Average in an SQL query

Asked

Viewed 360 times

4

I want to conduct a search in a query So far I have only been able to display the total value of the records. the idea is to show the spending average. Then it would be the (total / quantity) however I could not mount the logic: what I have so far presented the Count of all records

with AdoMEdOs do
begin
   Close;
   Sql.clear;
   AdoMEdOs.SQL.Add('select valor_total from ordem_servico');
   open;
   RecordCount;
   label5.Caption := IntToStr(AdoMEdOs.RecordCount) ;
   AdoMEdOs.Close;
end;

1 answer

4


This can be done using SQL or in the application. I recommend it by SQL as it is simpler and more effective (since the data is in the database). If this is done by SQL, then the aggregator function called avg, thus:

select avg(valor_total) as media from ordem_servico
  • label5.Caption := Inttostr(Adomedos.Recordcount) ; ?

  • @Guilhermelima, I changed the answer giving a name to the field, called media. Now you will use your bank access component, Adomedos, to get the value of the media field.

  • Pretty much what I needed !

  • made the query in sql and it returns me the value 24 and in Delphi returns me the value of 0 in the label.

  • @Guilhermelima, I haven’t dealt with Delphi for a long time. It’s certainly something you’re getting wrong. How you get the value of a field with this Fear component?

  • label5.Caption := Fieldbyname('media'). Asstring; Presented the value now, it was a mistake anyway.. thanks for the help.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.