You are here: LANSA for i User Guide > Appendix A. Virtual Fields > Examples of Virtual Fields & Derivation Code > Example 2: Accumulation Of Fields

Example 2: Accumulation Of Fields

A sales file contains monthly sales figures in real fields SAL01 to SAL12.

Generate a virtual field called SALYR to contain the total yearly sales and a virtual field called SALAV to contain the average monthly sales.

Also generate 4 virtual fields called SALQ1 -> SALQ4 to contain total quarterly sales.

Step 1 - Define the Virtual Fields

 

 

     Seq    Name__________ Description______________________

      10    SALYR          Total sales for year

      20    SALAV          Average monthly sales

      30    SALQ1          Total first quarter sales

      30    SALQ2          Total second quarter sales

      30    SALQ3          Total third quarter sales

      30    SALQ4          Total fourth quarter sales

 

Step 2 - Input RPG Section "Calculations After Input from File"

>

C*

C* VC_USING FIELDS(SAL01 SAL02 SAL03 SAL04 SAL05 SAL06 SAL07 

C*                 SAL08 SAL09 SAL10 SAL11 SAL12 SALYR)

C*

C* Calculate yearly sales

C*

C                     Z-ADDSAL01     SALYR

C                     ADD  SAL02     SALYR

C                     ADD  SAL03     SALYR

C                     ADD  SAL04     SALYR

C                     ADD  SAL05     SALYR

C                     ADD  SAL06     SALYR

C                     ADD  SAL07     SALYR

C                     ADD  SAL08     SALYR

C                     ADD  SAL09     SALYR

C                     ADD  SAL10     SALYR

C                     ADD  SAL11     SALYR

C                     ADD  SAL12     SALYR

C*

C* VC_USING FIELDS(SALYR SALAV)

C*

C* Calculate average monthly sales

C*

C           SALYR     DIV  12        SALAV

C*

C* VC_USING FIELDS(SAL01 SAL02 SAL03 SALQ1)

C*

C* Calculate first quarter sales

C*

C                     Z-ADDSAL01     SALQ1

C                     ADD  SAL02     SALQ1

C                     ADD  SAL03     SALQ1

C*

C* VC_USING FIELDS(SAL04 SAL05 SAL06 SALQ2)

C*

C* Calculate second quarter sales

C*

C                     Z-ADDSAL04     SALQ2

C                     ADD  SAL05     SALQ2

C                     ADD  SAL06     SALQ2

C*

C* VC_USING FIELDS(SAL07 SAL08 SAL09 SALQ3)

C*

C* Calculate third quarter sales

C*

C                     Z-ADDSAL07     SALQ3

C                     ADD  SAL08     SALQ3

C                     ADD  SAL09     SALQ3

C*

C* VC_USING FIELDS(SAL10 SAL11 SAL12 SALQ4)

C*

C* Calculate fourth quarter sales

C*

C                     Z-ADDSAL10     SALQ4

C                     ADD  SAL11     SALQ4

C                     ADD  SAL12     SALQ4

Note the use of multiple VC_USING commands in the source code. A VC_USING command remains in effect until another VC_USING is encountered or the end of the code section is encountered.