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
Antwort Antwort
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#1

calculating Rows in Table

  Alt 11. Aug 2012, 06:19
Datenbank: Absolute database • Version: 2011 • Zugriff über: SQL
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;
Daniel
  Mit Zitat antworten Zitat
Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#2

AW: calculating Rows in Table

  Alt 11. Aug 2012, 07:52
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 := ???
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat
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
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#4

AW: calculating Rows in Table

  Alt 11. Aug 2012, 12:38
sum.png
ABSDatabase1 => Table1 =>> DataSource1 =>> DBGrid1
Daniel
  Mit Zitat antworten Zitat
Perlsau
(Gast)

n/a Beiträge
 
#5

AW: calculating Rows in Table

  Alt 11. Aug 2012, 12:41
Anhang 37424
ABSDatabase1 => Table1 =>> DataSource1 =>> DBGrid1
Sorry, I don't understand your problem ...
  Mit Zitat antworten Zitat
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#6

AW: calculating Rows in Table

  Alt 11. Aug 2012, 15:09
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');
Daniel
  Mit Zitat antworten Zitat
Benutzerbild von p80286
p80286

Registriert seit: 28. Apr 2008
Ort: Stolberg (Rhl)
6.659 Beiträge
 
FreePascal / Lazarus
 
#7

AW: calculating Rows in Table

  Alt 11. Aug 2012, 15:29
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
Programme gehorchen nicht Deinen Absichten sondern Deinen Anweisungen
R.E.D retired error detector
  Mit Zitat antworten Zitat
Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#8

AW: calculating Rows in Table

  Alt 11. Aug 2012, 17:53
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 ...
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat
danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#9

AW: calculating Rows in Table

  Alt 11. Aug 2012, 18:40
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
Daniel
  Mit Zitat antworten Zitat
Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#10

AW: calculating Rows in Table

  Alt 11. Aug 2012, 19:02
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 ...
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat
Antwort Antwort


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 02:17 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