This example shows how to use the group processing functions to
summarize data from existing database records.
Assume that you want to group the data by customer number and
analyze the amount field. Your database file is FILEA and you create a file
named FILEAA containing a record format with the following fields:
FILEA |
FILEAA |
Cust |
Cust |
Type |
Count (count of records per customer) |
Amt |
Amtsum (summation of the amount field) |
|
Amtavg (average of the amount field) |
|
Amtmax (maximum value of the amount field) |
|
When you define the fields in the new file, you must ensure that
they are large enough to hold the results. For example, if the Amt field
is defined as 5 digits, you might need to define the Amtsum field as
7 digits. Any arithmetic overflow causes your program to end abnormally.
Assume that the records in FILEA have the following values:
Cust |
Type |
Amt |
001 |
A |
500.00 |
001 |
B |
700.00 |
004 |
A |
100.00 |
002 |
A |
1200.00 |
003 |
B |
900.00 |
001 |
A |
300.00 |
004 |
A |
300.00 |
003 |
B |
600.00 |
|
You then create a program (PGMG) using FILEAA as input to print
the records.
OVRDBF FILE(FILEAA) TOFILE(FILEA) SHARE(*YES)
OPNQRYF FILE(FILEA) FORMAT(FILEAA) KEYFLD(CUST) +
GRPFLD(CUST) MAPFLD((COUNT '%COUNT') +
(AMTSUM '%SUM(AMT)') +
(AMTAVG '%AVG(AMT)') +
(AMTMAX '%MAX(AMT)'))
CALL PGM(PGMG) /* Created using file FILEAA as input */
CLOF OPNID(FILEA)
DLTOVR FILE(FILEAA)
The records retrieved by the program appear as:
Cust |
Count |
Amtsum |
Amtavg |
Amtmax |
001 |
3 |
1500.00 |
500.00 |
700.00 |
002 |
1 |
1200.00 |
1200.00 |
1200.00 |
003 |
2 |
1500.00 |
750.00 |
900.00 |
004 |
2 |
400.00 |
200.00 |
300.00 |
|
Note: If you specify the GRPFLD parameter, the groups might not
appear in ascending sequence. To ensure a specific sequence, you should specify
the KEYFLD parameter.
Assume that you want to print only the summary records in this
example in which the
Amtsum value is greater than 700.00. Because the
Amtsum field
is an aggregate field for a given customer, use the GRPSLT parameter to specify
selection after grouping. Add the GRPSLT parameter:
GRPSLT('AMTSUM *GT 700.00')
The records retrieved by your program are:
Cust |
Count |
Amtsum |
Amtavg |
Amtmax |
001 |
3 |
1500.00 |
500.00 |
700.00 |
002 |
1 |
1200.00 |
1200.00 |
1200.00 |
003 |
2 |
1500.00 |
750.00 |
900.00 |
|
The Open Query File (OPNQRYF) command supports selection both
before grouping (QRYSLT parameter) and after grouping (GRPSLT parameter).
Assume that you want to select additional customer records in
which the
Type field is equal to A. Because
Type is a field
in the record format for file FILEA and not an aggregate field, you add the
QRYSLT statement to select before grouping as follows:
QRYSLT('TYPE *EQ "A" ')
Note: Fields used for selection do not have to appear in the format
processed by the program.
The records retrieved by your program are:
Cust |
Count |
Amtsum |
Amtavg |
Amtmax |
001 |
2 |
800.00 |
400.00 |
500.00 |
002 |
1 |
1200.00 |
1200.00 |
1200.00 |
|
Note: The values for CUST 001 changed because the selection took
place before the grouping took place.
Assume that you want to arrange the output
by the
Amtavg field in descending sequence, in addition to the previous
QRYSLT parameter value. You can do this by changing the KEYFLD parameter on
the OPNQRYF command as:
KEYFLD((AMTAVG *DESCEND))
The records retrieved by your program are:
Cust |
Count |
Amtsum |
Amtavg |
Amtmax |
002 |
1 |
1200.00 |
1200.00 |
1200.00 |
001 |
2 |
800.00 |
400.00 |
500.00 |
|