Henry Olive 5 Posted February 28, 2021 (edited) I wish everyone a healthy day. How can i get get SUM of a calculated field ? I have a TOTAL field which is calculated. I want to get SUM of TOTAL and write the result in a Label or DBtext p.s : I know i can do it While Not CDS1.Eof ... I wonder is there a better way Thank You Edited February 28, 2021 by Henry Olive Share this post Link to post
Henry Olive 5 Posted February 28, 2021 Thank you so much Dany, var MyCalcField : Double (CDS1 =Client Dataset;) MyCalcField :=(CDS1A.asFloat + CDS1B.asFloat + CDS1C.asFloat) - (CDS1D.asFloat + CDS1E.asFloat); Now i want to get Sum of MyCalcField (Total result of MyCalField) and I want to do this process in Delphi CDS1 View : EmpName MyCalcField AAA...........................5,000 BBB............................6,000 I need to get 5000 + 6000 = *11,000 result* I know i can make this with a SQL and i can make scan the CDS1 (While not CDS1.Eof do ... ) I thought may be there could be a better way to do this in Delphi Share this post Link to post
Guest Posted February 28, 2021 I use a different CDS. Generally Grids and "multi record" controls providing sums or summary functionality also loop through all records, if it would not it would not be able to load the records. Since you indicate a ClientDataSet, SQL would probably not yield a more performant result (it's executed locally). It could (if the CDS has that functionality) provide for better looking code depending on your background. KbmMemTable provides such functionality i do not know about TClientDataset. If it where a RDBMS connection, depending on the number of records, i sometimes use two requests, one for all the rows and one for an aggregated sum. But whether this is better/faster/... is a question of a lot of parameters. HTH, /D Share this post Link to post