Using the APIs in a Program

You can use the aggregate range routines whenever you would otherwise use E@DBFINDSUBRNG and E@DBFINDPRVRNG to access a set of rows for the purpose of applying average, maximum, minimum, or sum functions against a column.

Use the U@DB@AGINIT routine to initialize an aggregation request.

Use the E@DBCALCAGGRGT routine to process an aggregation request against a data set.

The database routines perform an equal condition on all D@ Index fields up to the subrange field designated in the E@DBSUBRNG. It performs a "where greater-than and less-than" condition on this index field.

  1. Set the D@<KeyField> as you would for any database inquiry routine.
  2. Express the ending value for the range key:

    EVAL D#END#<FieldName> = <identifier>

  3. Identify the range key by setting E@DBSUBRNG to a number that represents the position of the key field in the index.

    EVAL E@DBSUBRNG = <IndexPosition>

  4. Submit the request for an aggregation. This includes indicating which fields to aggregate and what function to apply.

    CALLP U@DB@AGSET(DGAM@@HANDLE: DA@DBGLGAM2:

    'SUM': 'CR-AMOUNT ': I2:

    %ADDR(DGAM#AG#CRAMNT(I2)):

    %ADDR(DGAM#SU#CRAMNT(I2)))

  5. Submit the request:

    CALLP DS@<Logical>(E@DBCALCAGGRGT)

  6. Test for function validity by referencing the COUNT variable:

    IF D<Prefix>@COUNT ...

  7. Un-reference the return values in the fields named D<Prefix>#AG#<FieldName>:

    Z-ADD D<Prefix>#AG#<FieldName> = <StorageField>