REPORT ZTEST. TYPES:BEGIN OF TY_DATA, NUMBER TYPE C, PRICE TYPE I, CATEGORY(20) TYPE C, END OF TY_DATA. TYPES:BEGIN OF TY_SUM, NUMBER TYPE C, PRICE TYPE I, END OF TY_SUM. DATA: LT_DATA TYPE TABLE OF TY_DATA, LT_SUM1 TYPE TABLE OF TY_SUM, LS_SUM1 TYPE TY_SUM, * LT_SUM2 TYPE TABLE OF TY_SUM, * LS_SUM2 TYPE TY_SUM, LT_SUM3 TYPE TABLE OF TY_SUM, LS_SUM3 TYPE TY_SUM, LT_SUM4 TYPE TABLE OF TY_SUM, LS_SUM4 TYPE TY_SUM. DATA: LV_SUM TYPE I. LT_DATA = VALUE #( ( NUMBER = 'a' PRICE = 23 CATEGORY = 'xxx') ( NUMBER = 'a' PRICE = 23 CATEGORY = 'xxx') ( NUMBER = 'a' PRICE = 23 CATEGORY = 'xxx') ( NUMBER = 'b' PRICE = 23 CATEGORY = 'xxx') ( NUMBER = 'b' PRICE = 23 CATEGORY = 'AAA') ( NUMBER = 'b' PRICE = 111 CATEGORY = 'AAA') ( NUMBER = 'b' PRICE = 12 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 23 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 21 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 11 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 11 CATEGORY = 'AAA') ). **1. *LOOP AT LT_DATA INTO DATA(LS_DATA). * LS_SUM1 = VALUE #( * NUMBER = LS_DATA-NUMBER * PRICE = LS_DATA-PRICE ). * * COLLECT LS_SUM1 INTO LT_SUM1. *ENDLOOP. * *CL_DEMO_OUTPUT=>DISPLAY( LT_SUM1 ). * *" 2. *SELECT NUMBER,SUM( PRICE ) AS PRICE * FROM @LT_DATA AS A * GROUP BY NUMBER * ORDER BY NUMBER ASCENDING * INTO TABLE @DATA(LT_SUM2). * *CL_DEMO_OUTPUT=>DISPLAY( LT_SUM2 ). * **3. * *LOOP AT LT_DATA INTO DATA(LS_DATA3) GROUP BY LS_DATA3-NUMBER ASSIGNING FIELD-SYMBOL(<KEY>). * LOOP AT GROUP <KEY> ASSIGNING FIELD-SYMBOL(<KEY_MEMBER>). * LS_SUM3-NUMBER = <KEY_MEMBER>-NUMBER. * LV_SUM = <KEY_MEMBER>-PRICE + LV_SUM. * ENDLOOP. * LS_SUM3-PRICE = LV_SUM. * APPEND LS_SUM3 TO LT_SUM3. * CLEAR: LV_SUM,LS_SUM3. *ENDLOOP. * *CL_DEMO_OUTPUT=>DISPLAY( LT_SUM3 ). *4 使用COLLECT 注意只能汇总数量金额字段 其他无关字段需要Clear 或者放到另一个结构中汇总 LOOP AT LT_DATA INTO DATA(LS_DATA). MOVE-CORRESPONDING LS_DATA TO LS_SUM4. COLLECT LS_SUM4 INTO LT_SUM4. ENDLOOP. CL_DEMO_OUTPUT=>DISPLAY( LT_SUM4 ).
标签:PRICE,汇总,NUMBER,语法,LT,LS,DATA,SUM From: https://www.cnblogs.com/Anyohh233/p/17980330