Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] REDUCE as an alternative to line_exists for the use of other comparison operators such as >, <, >=, <=, CS, CN

By using line_exists you can check if a line exists or not exists, but it is not possible to use other comparison operators. A workaround can be REDUCE in that case.

In the following snippet, we want to check if there is a flight that costs more than $1000,

1
2
3
4
5
6
7
8
SELECT * FROM sflight INTO TABLE @DATA(flights).
 
* Check if flight with price > 1000 exist
IF REDUCE abap_bool( INIT price_gt_1000 = abap_false
                     FOR  line IN flights WHERE ( price > 1000 )
                     NEXT price_gt_1000 = abap_true ) EQ abap_true.
  " flight with price > 1000 exists
ENDIF.

[ABAP] LOOP AT VALUE

Easy way to loop over multiple tables of the same type, without having to create an additional table to join the data before the loop.

1
2
3
4
5
6
7
8
9
SELECT FROM spfli FIELDS * WHERE carrid EQ 'AA' INTO TABLE @DATA(lt_spfli_1).
 
SELECT FROM spfli FIELDS * WHERE carrid EQ 'AZ' INTO TABLE @DATA(lt_spfli_2).
 
LOOP AT VALUE spfli_tab( ( LINES OF lt_spfli_1 )
                         ( LINES OF lt_spfli_2 )
                         ( carrid = 'AV' ) ) ASSIGNING FIELD-SYMBOL(<spfli>).
 WRITE <spfli>-carrid.
ENDLOOP.

Got this snippet from George Drakos talk from the ABAPConf 2024. Check his Github.

[ABAP] Sum column using reduce with where condition

1
2
3
4
5
SELECT * FROM sflight INTO TABLE @DATA(lt_sflight).
 
DATA(a319_paymant_sum) = REDUCE #( INIT sum = 0
                                   FOR flight IN lt_sflight WHERE ( planetype = 'A319' )
                                   NEXT sum += flight-paymentsum ).

[ABAP] Finding maximum value of specific column in itab

1
2
3
4
DATA(maximum) = REDUCE #( INIT max = VALUE myType( )
                          FOR  line IN myTable
                          NEXT max = COND #( WHEN line-myColumn > max THEN line-myColumn
                                                                      ELSE max ) ).