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?

bluish
  • 26,356
  • 27
  • 122
  • 180
DreadAngel
  • 772
  • 11
  • 30

1 Answers1

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.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • What about [`aggregate fields`](http://edn.embarcadero.com/article/29272) ? They seems to be better for this purpose. – TLama May 08 '12 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 08 '12 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 08 '12 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 08 '12 at 13:47