Cross-Tab reports are a very useful way of discovering statistical information about the data that you work with. They can be easily produced using the TABLE function and the aggregate functions (COUNT, SUM, MIN, MAX, AVE, VARIANCE, COVARIANCE, CORRELATION). The resulting recordset contains a single record for each unique value of the "group by" fields specified in the TABLE function, along with the statistics you generate with the aggregate functions.
The TABLE function's "group by" parameters are used and duplicated as the first set of fields in the RECORD structure, followed by any number of aggregate function calls, all using the GROUP keyword as the replacement for the recordset required by the first parameter of each of the aggregate functions. The GROUP keyword specifies performing the aggregate operation on the group and is the key to creating a Cross-Tab report. This creates an output table containing a single row for each unique value of the "group by" parameters.
The example code below (contained in the CrossTab.ECL file) produces an output of State/CountAccts with counts from the nested child dataset created by the GenData.ECL code (see the Creating Example Data article):
IMPORT $; Person := $.DeclareData.PersonAccounts; CountAccts := COUNT(Person.Accounts); MyReportFormat1 := RECORD State := Person.State; A1 := CountAccts; GroupCount := COUNT(GROUP); END; RepTable1 := TABLE(Person,MyReportFormat1,State,CountAccts ); OUTPUT(RepTable1); /* The result set would look something like this: State A1 GroupCount AK 1 7 AK 2 3 AL 1 42 AL 2 54 AR 1 103 AR 2 89 AR 3 2 */
Slight modifications allow some more sophisticated statistics to be produced, such as:
MyReportFormat2 := RECORD State{cardinality(56)} := Person.State; A1 := CountAccts; GroupCount := COUNT(GROUP); MaleCount := COUNT(GROUP,Person.Gender = 'M'); FemaleCount := COUNT(GROUP,Person.Gender = 'F'); END; RepTable2 := TABLE(Person,MyReportFormat2,State,CountAccts ); OUTPUT(RepTable2);
This adds a breakdown of how many men and women there are in each category, by using the optional second parameter to COUNT (available only for use in RECORD structures where its first parameter is the GROUP keyword).
The addition of the {cardinality(56)} to the State definition is a hint to the optimizer that there are exactly 56 values possible in that field, allowing it to select the best algorithm to produce the output as quickly as possible.
The possibilities are endless for the type of statistics you can generate against any set of data.