AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Datenbanken Delphi calculating Rows in Table
Thema durchsuchen
Ansicht
Themen-Optionen

calculating Rows in Table

Ein Thema von danten · begonnen am 11. Aug 2012 · letzter Beitrag vom 11. Aug 2012
 
Perlsau
(Gast)

n/a Beiträge
 
#3

AW: calculating Rows in Table

  Alt 11. Aug 2012, 08:15
Hi friends, I need help with counting rows in database table.
The result should appear in the Edit.

for example:
Col1 = AsString
ROW1 = 345,50
ROW2 = 28,90
row3 = 13,60
row
.
.
row
rowx = 1,08


Code:
TForm1.Button1Click procedure (Sender: TObject);
boiling
  i: integer
  y: double;
begin
    for i: = -1 to Table1.DataSet.RecordCount -1
begin
   y: = Calculate (Table1.Rows);
end;
    Edit1.Text: = FloatToStr(y);
end;
Sorry, I can't see your problem. Or better said: I can see you have a lot of problems with understanding Delphi.

I think you have put a table component on your form which represents the contents of the table in your database. I don't know which table component has got a property named dataset ... What is the name of your database components?

To count means to find out how much rows a table has. You already know the property RecordCount, it reprsents the count of rows.

When you try to make a loop through all records in a table, never use RecordCount, because it only shows you the count of the already fetched records. Better you ask for "End of file":

Delphi-Quellcode:
Dataset.First;

WHILE NOT Dataset.Eof DO
BEGIN
     {do something}

     Dataset.Next;
END;
But I think you don't want to count rows. You want to calculate a sum or compute average. For the last posibility you first have to calculate the sum of the fields and store it in a local variable. After ending the loop you divide the sum with the count of records as divisor:

Delphi-Quellcode:
PROCEDURE MakeAverage;
VAR
   MyAverage,
   MySum : Currency;
   MyCount : Integer;

BEGIN
     MyCount := 0;
     MySum := 0;

     Dataset.First;

     WHILE NOT Dataset.Eof DO
     BEGIN
          INC(MyCount);
          MySum := MySum + Dataset.FieldByName('SumField').AsCurrency;
          Dataset.Next;
     END;

     MyAverage := MySum / MyCount;
END;

Geändert von Perlsau (11. Aug 2012 um 08:18 Uhr)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:39 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz