Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi calculating Rows in Table (https://www.delphipraxis.net/169782-calculating-rows-table.html)

danten 11. Aug 2012 06:19

Datenbank: Absolute database • Version: 2011 • Zugriff über: SQL

calculating Rows in Table
 
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;

Bummi 11. Aug 2012 07:52

AW: calculating Rows in Table
 
What are you looking for?

AA 1 2 3 4 5 6 7 >>> 28
BB 3 2 3 4 3 5 4 >>> 24
XY 1 2 3 1 2 3 1 >>> 13
------------------------
5 6 9 9 10 14 12 >>> 65

EDIT1.Text := ???

Perlsau 11. Aug 2012 08:15

AW: calculating Rows in Table
 
Zitat:

Zitat von danten (Beitrag 1177786)
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;

danten 11. Aug 2012 12:38

AW: calculating Rows in Table
 
Liste der Anhänge anzeigen (Anzahl: 1)
Anhang 37424
ABSDatabase1 => Table1 =>> DataSource1 =>> DBGrid1

Perlsau 11. Aug 2012 12:41

AW: calculating Rows in Table
 
Zitat:

Zitat von danten (Beitrag 1177812)
Anhang 37424
ABSDatabase1 => Table1 =>> DataSource1 =>> DBGrid1

Sorry, I don't understand your problem ...

danten 11. Aug 2012 15:09

AW: calculating Rows in Table
 
Calculate all DBGrid Rows 'Cena' (price).
'Cena' AsString;
The result written to Edit.


Delphi-Quellcode:
function Calculate_(table: TABSTable; Field: string):string;
var
  celkem: string;
  tpRunTotal: string;
begin
  result := '0';
  tpRunTotal := '0,00';
with Table do
begin
  First;
  while not EOF do
  begin
    tpRunTotal := FloatToStr((StrToFloat(tpRunTotal) + (StrToFloat(Table.FieldByName(field).AsString))));
    celkem := FormatFloat('0.00', StrToFloat(tpRunTotal));
    result := celkem;
    Next;
  end;
end;
end;

Example:
dm := TDataModule;
prodej_distributor := TABSTable;
Edit1.Text := Calculate_(dm.prodej_distributor,'Cena');

p80286 11. Aug 2012 15:29

AW: calculating Rows in Table
 
Just an other idea.
try it with two queries. First
Code:
query1.SQL.Text:='select * from mytable';
instead of table1.
and second
Code:
query2.SQL.Text:='select sum(mycol) from mytable';

Greetings
K-H

Bummi 11. Aug 2012 17:53

AW: calculating Rows in Table
 
Zitat:

tpRunTotal := FloatToStr((StrToFloat(tpRunTotal) + (StrToFloat(Table.FieldByName(field).AsString))));
uargh ...... whats that ....
Query with SUM as described is the better solution, but if you want to use something like that, then use a double or currency for tpRunTotal and convert it after the loop ...

danten 11. Aug 2012 18:40

AW: calculating Rows in Table
 
Zitat:

Zitat von Bummi (Beitrag 1177840)
Zitat:

tpRunTotal := FloatToStr((StrToFloat(tpRunTotal) + (StrToFloat(Table.FieldByName(field).AsString))));
uargh ...... whats that ....
Query with SUM as described is the better solution, but if you want to use something like that, then use a double or currency for tpRunTotal and convert it after the loop ...

Sorry but I do not understand what you mean

Bummi 11. Aug 2012 19:02

AW: calculating Rows in Table
 
Delphi-Quellcode:
function Calculate_(table: TABSTable; Field: string):string;
var
  tpRunTotal: Double;
begin
tpRunTotal := 0;
with Table do
begin
  First;
  while not EOF do
  begin
    tpRunTotal := tpRunTotal + Table.FieldByName(field).AsFloat;
    Next;
  end;
end;
Result := FormatFloat('0.00',tpRunTotal )
end;
but mentioned Query with SUM will be better ...


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:37 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz