ABAP CDS中带货币转换的计算字段求平均值问题
ABAP CDS中货币字段平均值计算与货币转换解决方案
可以在计算字段中使用currency_conversion函数,但不能直接传入字面常量(比如'1'),因为该函数的AMOUNT参数仅支持字段、路径或CDS参数。以下是适配你现有方案的具体解决步骤:
1. 调整基础汇总视图
先确保基础视图中正确汇总金额和计数,同时保留源币种信息:
@EndUserText.label: '发票基础汇总视图' define view Z_V_FAKTURA_SUM as select from your_faktura_table { key faktura_id, sum( your_currency_field ) as total_amount, // 金额总和,@DefaultAggregation: #SUM sum( 1 ) as record_count, // 记录计数,@DefaultAggregation: #SUM your_curr_code as source_curr // 源币种字段 } group by faktura_id, your_curr_code;
2. 在上层视图实现平均值+货币转换
通过计算字段先算出平均值,再对该字段执行货币转换,或直接在currency_conversion中传入平均值计算表达式:
@EndUserText.label: '带货币转换的发票平均金额视图' define view Z_V_FAKTURA_AVG_CURR with parameters p_target_curr : abap.cuky(5) // 目标币种参数 as select from Z_V_FAKTURA_SUM { faktura_id, // 先计算平均值作为中间字段 (total_amount / record_count) as avg_amount, // 对平均值执行货币转换 currency_conversion( amount => avg_amount, source_currency => source_curr, target_currency => :p_target_curr, exchange_rate_date => sy-datum, exchange_rate_type => 'M' // 根据实际需求维护汇率类型 ) as avg_amount_target_curr };
关键注意事项
- 避免直接在
currency_conversion的amount参数中写字面常量,必须传递字段、CDS参数或路径。如果需要固定值,可在基础视图中定义一个固定字段(如dummy_val : abap.fltp := 1.0;)再引用。 - 若需在Analytics Engine的查询公式中集成,建议将货币转换逻辑封装到CDS计算字段中,而非直接写在
@AnalyticsDetails.query.formula里,因为查询公式支持的函数范围有限。
内容的提问来源于stack exchange,提问作者toffler
相关产品推荐
相关产品推荐

