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?

1 Answer 1

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.

4
  • What about aggregate fields ? They seems to be better for this purpose.
    – TLama
    May 8, 2012 at 11:39
  • 3
    @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.
    – Uwe Raabe
    May 8, 2012 at 11:50
  • 3
    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.
    – Uwe Raabe
    May 8, 2012 at 13:41
  • 1
    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?
    – DreadAngel
    May 8, 2012 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.