![]() |
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; |
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 := ??? |
AW: calculating Rows in Table
Zitat:
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:
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:
Dataset.First;
WHILE NOT Dataset.Eof DO BEGIN {do something} Dataset.Next; END;
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; |
AW: calculating Rows in Table
Liste der Anhänge anzeigen (Anzahl: 1)
Anhang 37424
ABSDatabase1 => Table1 =>> DataSource1 =>> DBGrid1 |
AW: calculating Rows in Table
Zitat:
|
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'); |
AW: calculating Rows in Table
Just an other idea.
try it with two queries. First
Code:
instead of table1.
query1.SQL.Text:='select * from mytable';
and second
Code:
query2.SQL.Text:='select sum(mycol) from mytable';
Greetings K-H |
AW: calculating Rows in Table
Zitat:
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 ... |
AW: calculating Rows in Table
Zitat:
|
AW: calculating Rows in Table
Delphi-Quellcode:
but mentioned Query with SUM will be better ...
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; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:44 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 by Thomas Breitkreuz