Xbasic

RUN_COUNT Function

Syntax

Running_Count_Value as A = run_count(A subexpr [,G over [,G each]])

Arguments

subexprAny Type

Any valid expression that includes a field or a combination of fields from the current table or set that specifies what you want to examine.

overGRP->Group

When to reset the count to zero. Set GRP->Grand to count over the entire report.

eachGRP->Sub_Group

What item to count (e.g. GRP->INV_NO to count each INV_NO in the group.)

Description

Get the running count of subexpression over group 'over' sampled each group 'each'

Discussion

The Group and Sub_Group are optional parameters that define the range of records to summarize, and may or may not be required, depending upon the context of the summary calculation. Possible values for GRP->Group and GRP->Sub_Group include:

  • GRAND (all records)

  • The name of the current table

  • The name of a table in the current set

  • The name of a grouping level that is defined in a report layout

  • DETAIL (over the records in the detail section)

RUN_COUNT() is used in the Detail section of a report to return the running count of records of the Expression evaluated from the first record to the current record related to the group. The expression is evaluated from the sub-group's first record to the current record, and the resulting count is returned.

For example, to summarize the line-items in an invoice set, the Group parameter might be GRP->INV_HEAD and the Sub_Group parameter GRP->INV_ITEM. This means the Summary operation includes only those child records in INV_ITEM related to the current parent record in the INV_HEAD table. To summarize all the records in a table or set, use the name GRAND as the Group name. The Group GRP->GRAND and Sub_Group GRP->INV_ITEM will produce a summary result based on all invoices.

This function is a report writer function, not intended for table level field rules or other expressions. While the function may perform in some areas outside of the report writer, its use there is not supported.

Examples 

Assume that an invoicing set consists of two tables. One holds the invoices (INVOICE), and the other holds each invoice's line-items (INV_ITEM):

INVOICE

INV_NO

CUST_ID

INV_TOTAL

I100

C001

10.00

I101

C003

25.50

I102

C001

100.75

I103

C004

32.50

I104

C002

110.25

I105

C005

98.35

INV_ITEM

INV_NO

AMOUNT

I100

4.50

I100

5.50

I101

25.50

I102

3.25

I102

35.25

I102

62.25

I103

32.50

I104

110.25

I105

98.35

INVOICE is related to INV_ITEM through a one-to-many link on the INV_NO field. To find the running count of line-item records, use the following expressions:

run_count(INV_ITEM->AMOUNT) -> 9, if the current INV_NO is "I105"
run_count(INV_ITEM->AMOUNT) -> 8, if the current INV_NO is "I104"

Using run_count() in Groups 

If you wanted to produce a running count of all Invoices, you could define the following calculated field that would create a running count of each INVOICE (as opposed to each line item in each INVOICE.) In the example below, it's assumed that a Group Header has been defined in the Report that breaks on the INVOICE->INV_NO field:

invoiceCount = run_count(INVOICE->INV_NO,GRP->Grand,GRP->INV_NO)

This code creates a calculated field that computes a running total over the entire report (GRP->Grand) that is incremented every time the invoice number changes (GRP->INV_NO).

Once created, the calculated field can be inserted into the Group Header where desired.

In the example below, run_count() has been used to create counts for two groups: Country and City. Each country is enumerated with a number, as well as each city. The count resets to zero for the City every time a new Country is encountered. Two calculated fields were created with the following definition to generate the counts:

countryCount = run_count(country,GRP->Grand,GRP->Country)
cityCount = run_count(city,GRP->Country,GRP->City)

The calculated fields were then placed in the Group Header for the Country and City groups, as shown in the image below.

images/run_count1.png

The result is a report with numbered countries and cities, with the customers for each city listed in the detail:

images/run_count2.png

You can click here to download the report used in this example.

Limitations

This function is a report writer function, not intended for table level field rules or other expressions.

See Also