ABAP中SELECT语句中的计算字段

问题描述 投票:0回答:2

ABAP支持SELECT语句中的计算字段吗?如:

SELECT price quantity price*quantity AS sum
FROM ...
INTO ...
WHERE ...

如果没有,请让高级程序员告诉我他们使用什么解决方案来应对此任务。 不要提供诸如在 SELECT 周期内计算到临时变量等降低性能的方法。

SELECT.
...
ENDSELECT.
select abap
2个回答
2
投票

Open SQL 仅支持几个聚合函数。选择记录并将结果保存到内部表格后,应在内存中进行计算。
简而言之:

select ... into table itab from ...
loop at itab assigning <itab_row>.
  " Perform calculations on row here
endloop.

0
投票

同时,ABAP支持SQL语句内的算术运算。

您可以直接使用

+
-
*
。对于
/
,您需要首先转换为固有的 ABAP 类型:

select carrid, seatsocc, seatsmax,
       seatsmax - seatsocc as empty_seats,
       cast( seatsocc as D16N ) / cast( seatsmax as D16N ) as utilization
  from sflights
  into table @data(lt_flights).

最常见的用例是将其与分组结合并构建总和:

select carrid,
       sum( seatsmax - seatsocc ) as empty_seats,
       sum( cast( seatsocc as d16n ) / cast( seatsmax as d16n ) ) as utilization
  from sflights
  group by carrid
  into table @data(lt_flights).
© www.soinside.com 2019 - 2024. All rights reserved.