语法
reduce 类型 ( 初始化定义 结构定义 条件 运算)
1 2 3 4 5 6 7 8 9 10 11 12 |
REDUCE type( [let_exp] INIT {x1 = rhs1}|{ = wrexpr1}|{x1| TYPE dtype1} {x2 = rhs2}|{ = wrexpr2}|{x2| TYPE dtype2} ... FOR for_exp1 FOR for_exp2 ... NEXT ... {x1 = rhs1}|{ = wrexpr1} {x2 = rhs2}|{ = wrexpr2} ... ) ... |
简单例子
1 2 3 4 5 6 7 |
"内表行数 DATA(lv_lines) = lines( gt_man ). "内表中符合条件的数据有几条,WHERE是条件 FOR是结构 NEXT是逻辑 DATA(lv_lines_g) = REDUCE i( INIT x = 0 FOR ls_man IN gt_man WHERE ( sex = 'G' ) NEXT x = x + 1 ). |
综合例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TYPES:BEGIN OF ty_result, sum TYPE p DECIMALS 2, "总和 max TYPE p DECIMALS 2, "最大值 avg TYPE p DECIMALS 2, "平均 cunt TYPE i, "记录数 END OF ty_result. DATA(ls_result) = REDUCE ty_result( INIT res = VALUE ty_result( ) "可以默认值:ty_result( min = 0 max = 0 ) FOR IN gt_man WHERE ( sex = 'G' ) "性别为G NEXT res-sum = res-sum + -age "年龄总和 res-max = nmax( val1 = res-max val2 = -age )"最大年龄 res-cunt = res-cunt + 1 "满足条件的条目数 ). ls_result-avg = ls_result-sum / ls_result-cunt. "平均值 cl_demo_output=>write( ls_result ). cl_demo_output=>display( ). |