13.1.2 Field Groups and Expandable Groups
The GROUP_BY command is used to group one or more fields under a common name. It is one of the most time saving of all the RDML commands because it saves having to repeatedly specify a long list of field names.
Most commands that require a list of field names as a parameter also allow a group name to be specified. Consider the following example:
BEGIN_LOOP
REQUEST FIELDS(#ORDLIN #PRODUCT #QUANTITY #PRICE)
INSERT FIELDS(#ORDLIN #PRODUCT #QUANTITY #PRICE)
TO_FILE(A)
INSERT FIELDS(#ORDLIN #PRODUCT #QUANTITY #PRICE)
TO_FILE(B)
UPRINT FIELDS(#ORDLIN #PRODUCT #QUANTITY #PRICE)
CHANGE FIELD(#ORDLIN #PRODUCT #QUANTITY #PRICE)
TO(*DEFAULT)
END_LOOP
Now consider the identical RDML program written using a GROUP_BY command to group all the fields under a common name:
GROUP_BY NAMED(#ORDERLINE) FIELDS(#ORDLIN #PRODUCT #QUANTITY
#PRICE)
BEGIN_LOOP
REQUEST FIELDS(#ORDERLINE)
INSERT FIELDS(#ORDERLINE) TO_FILE(A)
INSERT FIELDS(#ORDERLINE) TO_FILE(B)
UPRINT FIELDS(#ORDERLINE)
CHANGE FIELD(#ORDERLINE) TO(*DEFAULT)
END_LOOP
If 5 new fields were to be added to the RDML program which would be the easiest to change?
Some points to note about groups and the GROUP_BY command are:
- As many GROUP_BY commands as desired, can be declared.
- A field can be declared in the FIELDS parameter of multiple GROUP_BY commands.
- Generally a GROUP_BY name can be used wherever a list of field names can be specified.
- If a GROUP_BY name is declared like this:
GROUP_BY NAMED(#ORDERHEAD) FIELDS(#ORDER #CUSTNO #ADDR1
#ADDR2
#POSTCD)
and then used like this:
FETCH FIELDS(#ORDERHEAD) FROM_FILE(ORDHED)
FETCH FIELDS(#ORDERHEAD) FROM_FILE(CUSMST)
then the first FETCH will only retrieve fields in the group that come from the ORDHED file. In this case only #ORDER, and #CUSTNO will be fetched. All other fields in the group are unchanged / ignored by the command because they don't come from the ORDHED file.
The second FETCH will retrieve the fields #CUSTNO (again), #ADDR1, #ADDR2 and #POSTCD because they all come from the CUSMST file. Field #ORDER will be unchanged / ignored by this command as it does not come from the CUSMST file.
Also See