以下是用ABAP语言编写的示例代码,可以实现按成本中心分组并计算员工总和:
首先,需要一个包含员工姓名、成本中心和工资信息的表:
DATA: it_employee TYPE STANDARD TABLE OF (staff_name TYPE string, cost_center TYPE string, salary TYPE i).
将数据添加到表中:
APPEND VALUE #( staff_name = 'John Smith' cost_center = 'CC1' salary = 5000 ) TO it_employee, VALUE #( staff_name = 'Jane Doe' cost_center = 'CC1' salary = 6000 ) TO it_employee, VALUE #( staff_name = 'Bob Johnson' cost_center = 'CC2' salary = 7000 ) TO it_employee, VALUE #( staff_name = 'Mary Smith' cost_center = 'CC2' salary = 8000 ) TO it_employee.
现在进行按成本中心分组并计算员工总和:
DATA: lt_grouped_employee TYPE SORTED TABLE OF it_employee WITH UNIQUE KEY cost_center.
"按成本中心分组并计算总和 LOOP AT it_employee INTO DATA(ls_employee). ADD ls_employee-salary TO lt_grouped_employee[ cost_center = ls_employee-cost_center ]-salary. ENDLOOP.
"输出结果 LOOP AT lt_grouped_employee INTO DATA(ls_grouped_employee). WRITE: / 'Cost Center:', ls_grouped_employee-cost_center, 'Total Salary:', ls_grouped_employee-salary. ENDLOOP.
结果:
Cost Center: CC1 Total Salary: 11000 Cost Center: CC2 Total Salary: 15000
通过上述代码,我们成功地按成本中心分组并计算了员工总和。