0

how can we achieve REDUCE like the below logic inside an AMDP method.

lv_total = reduce tslxx9( init x type tslxx9 for lwa_out in lt_out
                              where ( companycode     = <lf_wa>-bukrs and
                                      ryear           = <lf_wa>-year  and
                                      currency        = <lf_wa>-currency
                                    )
                                      next x = x + lwa_out-amount ).
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • AMDP is usually HANA SQL Script. If you refer to ABAP to describe what you want to achieve, you will reach less people, so I recommend that you describe your goal with words and example. – Sandra Rossi Dec 17 '21 at 19:09

1 Answers1

1

The direct translation to SQLScript would be to perform a SELECT on a local table variable with a GROUP BY clause and the SUM aggregation function. Something like this:

  METHOD sum_amdp BY DATABASE PROCEDURE FOR HDB LANGUAGE SQLSCRIPT.
     lv_total = SELECT SUM( amount )
       FROM :it_out
       WHERE companycode = :lf_wa.bukrs
         AND ryear = :lf_wa.year
         AND currency = :lf_wa.currency
       GROUP BY companycode, ryear, currency;
  ENDMETHOD.

This method is of course rather pointless to implement in AMDP because it doesn't even access the database. But it demonstrates how SQLScript is able to perform complex SQL statements on variables. Which only makes sense in the context of a larger SQLScript method which either does this to process data from a database query it just performed or to prepare data for a database query it is going to perform.

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • `group by` may be removed as you've already filtered required `companycode`, `ryear` and `currency`. And AMDP method's parameters [may be either of elementary type or of table type](https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/index.htm), structure is not allowed (HANA doesn't have appropriate native type). Anyway, as you've already mentioned, it's pointless to push internal table into the database just for calculation on it (especially for row-level processing). – astentx Jan 06 '22 at 22:51