Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Math between multiple REDUCE

DoanManhQuynh
Active Contributor
0 Kudos
598

Hi.

I want to count distinct value in 2 difference internal table then sum so I do as below:

CNT_FINAL = ( REDUCE I( INIT CNT1 = 0
                        FOR GROUPS OF M1 IN GRP1 GROUP BY M1-ID
                        NEXT CNT1 = CNT1 + 1 ) ) +
            ( REDUCE I( INIT CNT2 = 0
                        FOR GROUPS OF M2 IN GRP2 GROUP BY M2-ID
                        NEXT CNT2 = CNT2 + 1 ) ).

but the result is not correct. if i change like this ( calculation in 2 steps ), it correct:

CNT_FINAL = REDUCE I( INIT CNT1 = 0
                      FOR GROUPS OF M1 IN GRP1 GROUP BY M1-ID
                      NEXT CNT1 = CNT1 + 1 ).
CNT_FINAL = CNT_FINAL +
            REDUCE I( INIT CNT2 = 0
                      FOR GROUPS OF M2 IN GRP2 GROUP BY M2-ID
                      NEXT CNT2 = CNT2 + 1 ).

so I don't know is there any restrict to do the calculation between 2 REDUCE or not.

My pseudo code:

DATA CNT_FINAL TYPE I.
TYPES: BEGIN OF TY_GRP,
         ID  TYPE C,
         DAT TYPE C,
       END OF TY_GRP.
DATA: GRP1 TYPE TABLE OF TY_GRP,
      GRP2 TYPE TABLE OF TY_GRP.
GRP1 = VALUE #( ( ID = 1 DAT = 'a' )
                ( ID = 1 DAT = 'x' )
                ( ID = 2 DAT = 'b' ) ).
GRP2 = VALUE #( ( ID = 3 DAT = 'a' )
                ( ID = 4 DAT = 'b' ) ).
" Correct
CNT_FINAL = REDUCE I( INIT CNT1 = 0
                      FOR GROUPS OF M1 IN GRP1 GROUP BY M1-ID
                      NEXT CNT1 = CNT1 + 1 ).
CNT_FINAL = CNT_FINAL +
            REDUCE I( INIT CNT2 = 0
                      FOR GROUPS OF M2 IN GRP2 GROUP BY M2-ID
                      NEXT CNT2 = CNT2 + 1 ).
BREAK-POINT.
" Not correct
CLEAR CNT_FINAL.
CNT_FINAL = ( REDUCE I( INIT CNT1 = 0
                        FOR GROUPS OF M1 IN GRP1 GROUP BY M1-ID
                        NEXT CNT1 = CNT1 + 1 ) ) +
            ( REDUCE I( INIT CNT2 = 0
                        FOR GROUPS OF M2 IN GRP2 GROUP BY M2-ID
                        NEXT CNT2 = CNT2 + 1 ) ).
BREAK-POINT.
0 REPLIES 0