Aggregating a calculated field in TClientDataset
Asked Answered
S

1

6

Is it possible to add an aggregate field over a calculated field?

Suppose there is a cds with following fields:

  • ID(autoincrement)
  • Name(string)
  • Price(float)
  • Quantity(integer)
  • Total(integer) - calculated field - Price * Quantity

and I want to add an agregate to get the sum of Total column. Is it possible?

Supremacist answered 8/5, 2012 at 11:3 Comment(0)
E
5

Yes, that is possible if you change the calculated field to an InternalCalc field. The calculation is still done in the OnCalcFields event, but you have to check the state for InternalCalc when you make the calculation for that field.

Endothecium answered 8/5, 2012 at 11:34 Comment(4)
What about aggregate fields ? They seems to be better for this purpose.Particularity
@TLama, of course you can make an aggregate field with SUM(Price*Quantity), but the question was how to make an aggregate over a calculated field. This will only work if the calculated field has FieldKind = fkInternalCalc.Endothecium
It is mainly the sequence of evaluation: InternalCalc fields are calcluated before aggregating and sorting, so they can be used in aggregates and as an index. Calculated fields are evaluated after that. InternalCalc fields are only supported in TClientDataSets.Endothecium
Could You explain me what is the difference between InternalCalc and Calculated field. In the context that I want to use it's value in an agregate (as I asked) and meanwhile showing the calculated value into grid?Supremacist

© 2022 - 2024 — McMap. All rights reserved.