Display the Average in an SQL query

4

I want to perform a search on a query so far I have only been able to display the total value of the logs.  the idea is to show the average spending. Then it would be the (total / quantity) but I could not mount the logic: what I have so far that presents the count of all the 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;
    
asked by anonymous 25.11.2015 / 13:52

1 answer

3

This can be done using SQL or in the application. I recommend SQL because it is simpler and more effective (since the data is in the database). If it is done by SQL, then you should use the aggregate function called avg , like this:

select avg(valor_total) as media from ordem_servico
    
25.11.2015 / 14:01