内表的行列转换.

一个网友写的例子.


REPORT  Z_MATRIX                                .
***加数据的宏,没什么说的
DEFINE ADD_DATA .
  RTAB1-NUM = &1 .
  RTAB1-0 = &2 .
  RTAB1-1 = &3 .
**如果需要转置的表就两列,把这个去掉
  RTAB1-2 = &4 .
APPEND RTAB1 TO ITAB1 .
END-OF-DEFINITION .
***因为是模拟矩阵的转置,所以列定义为数字
***需要转置的内表
DATA: BEGIN OF RTAB1 ,
        NUM(4) TYPE C ,
        0(4) TYPE C ,
        1(4) TYPE C ,
********如果两列,去掉下面的
        2(4) TYPE C ,
      END OF RTAB1 .
**这是转置之后的表结构,找10列做的,可以自己修改
DATA: BEGIN OF RTAB2 ,
        NUM(4) TYPE C ,
        0(4) TYPE C ,
        1(4) TYPE C ,
        2(4) TYPE C ,
        3(4) TYPE C ,
        4(4) TYPE C ,
        5(4) TYPE C ,
        6(4) TYPE C ,
        7(4) TYPE C ,
        8(4) TYPE C ,
        9(4) TYPE C ,
      END OF RTAB2 .
**把要转置的表定义成排序表
DATA: ITAB1 LIKE SORTED TABLE OF RTAB1 WITH UNIQUE KEY NUM 0 .
DATA: ITAB2 LIKE STANDARD TABLE OF RTAB2 .
DATA: ITAB1_TEMP LIKE RTAB1 OCCURS 0 WITH HEADER LINE .
***变量定义
DATA: L_INDEX TYPE I ,    “表索引
      L_TEMP(18) TYPE C , “表元素的名称
      L_X TYPE I ,        “表的行数
      L_H(2) TYPE C ,     “表的行
      L_V(2) TYPE C ,     “表的列
      L_CY(2) TYPE C .    “表的列的字符型
***这里是表的列数
CONSTANTS: L_COUN TYPE I VALUE 3 ,
***********这个是表的列数减 1
           L_NUM  TYPE I VALUE 2 .
***这个是表元素的指针
FIELD-SYMBOLS: TYPE ANY ,
               TYPE ANY .
***这个是表结构的指针
FIELD-SYMBOLS: LIKE RTAB1 ,
               LIKE RTAB2 .
***如果是两列,用下面注释的数据玩
*ADD_DATA '1000' 'AA' '1.1' .
*ADD_DATA '1000' 'BB' '2.1' .
*ADD_DATA '1000' 'CC' '3.1' .
*ADD_DATA '1000' 'DD' '4.1' .
*ADD_DATA '2000' 'AA' '1.2' .
*ADD_DATA '2000' 'BB' '2.2' .
*ADD_DATA '2000' 'CC' '3.2' .
*ADD_DATA '2000' 'DD' '4.2' .
*ADD_DATA '3000' 'AA' '1.3' .
*ADD_DATA '3000' 'BB' '2.3' .
*ADD_DATA '3000' 'CC' '3.3' .
*ADD_DATA '3000' 'DD' '4.3' .
*ADD_DATA '3000' 'EE' '5.3' .
*ADD_DATA '3000' 'FF' '6.3' .
*ADD_DATA '3000' 'GG' '7.3' .
*ADD_DATA '4000' 'AA' '1.4' .
*ADD_DATA '4000' 'BB' '2.4' .
*ADD_DATA '4000' 'CC' '3.4' .
*ADD_DATA '4000' 'DD' '4.4' .
*ADD_DATA '4000' 'EE' '5.4' .
*ADD_DATA '5000' 'AA' '1.5' .
*ADD_DATA '5000' 'BB' '2.5' .
*ADD_DATA '5000' 'CC' '3.5' .
*ADD_DATA '5000' 'DD' '4.5' .
*ADD_DATA '5000' 'EE' '5.5' .
*ADD_DATA '5000' 'FF' '6.5' .
*ADD_DATA '5000' 'GG' '7.5' .
*ADD_DATA '5000' 'HH' '8.5' .
*ADD_DATA '5000' 'II' '9.5' .
***如果是三列,用这个数据玩
ADD_DATA '1000' 'AA' '11.1' '12.1'.
ADD_DATA '1000' 'BB' '11.2' '12.2'.
ADD_DATA '1000' 'CC' '11.3' '12.3'.
ADD_DATA '1000' 'DD' '11.4' '12.4'.
ADD_DATA '2000' 'AA' '21.1' '22.1'.
ADD_DATA '2000' 'BB' '21.2' '22.2'.
ADD_DATA '2000' 'CC' '21.3' '22.3'.
ADD_DATA '2000' 'DD' '21.4' '22.4'.
ADD_DATA '3000' 'AA' '31.1' '32.1'.
ADD_DATA '3000' 'BB' '31.2' '32.2'.
ADD_DATA '3000' 'CC' '31.3' '32.3'.
ADD_DATA '3000' 'DD' '31.4' '32.4'.
ADD_DATA '3000' 'EE' '31.5' '32.5'.
ADD_DATA '3000' 'FF' '31.6' '32.6'.
ADD_DATA '4000' 'AA' '41.1' '42.1'.
ADD_DATA '4000' 'BB' '41.2' '42.2'.
ADD_DATA '4000' 'CC' '41.3' '42.3'.
ADD_DATA '4000' 'DD' '41.4' '42.4'.
ADD_DATA '4000' 'EE' '41.5' '42.5'.
ADD_DATA '5000' 'AA' '51.1' '52.1'.
ADD_DATA '5000' 'BB' '51.2' '52.2'.
ADD_DATA '5000' 'CC' '51.3' '52.3'.
ADD_DATA '5000' 'DD' '51.4' '52.4'.
ADD_DATA '5000' 'EE' '51.5' '52.5'.
ADD_DATA '5000' 'FF' '51.6' '52.6'.
ADD_DATA '5000' 'GG' '51.7' '52.7'.
ADD_DATA '5000' 'HH' '51.8' '52.8'.
***其实就是初始化目标表
LOOP AT ITAB1 INTO RTAB1 .
  AT END OF NUM .
****向第一行加一个空数据,有几个不同的num就加几个
    RTAB2-NUM = '' .
    APPEND RTAB2 TO ITAB2 .
****有几个相同的num就加几个
    DO L_NUM TIMES .
      RTAB2-NUM = RTAB1-NUM .
      APPEND RTAB2 TO ITAB2 .
    ENDDO .
  ENDAT .
ENDLOOP .
***初始化行列数
L_H = '0' .
L_V = '1' .
L_CY = '0' .
***处理开始了
LOOP AT ITAB1 INTO RTAB1 .
  APPEND RTAB1 TO ITAB1_TEMP .
  AT END OF NUM .
****向第一行加一个空数据,有几个不同的num就加几个
    RTAB2-NUM = '' .
    APPEND RTAB2 TO ITAB2 .
****有几个相同的num就加几个
    DO L_NUM TIMES .
      RTAB2-NUM = RTAB1-NUM .
      APPEND RTAB2 TO ITAB2 .
    ENDDO .
  ENDAT .
ENDLOOP .
***初始化行列数
L_H = '0' .
L_V = '1' .
L_CY = '0' .
***处理开始了
LOOP AT ITAB1 INTO RTAB1 .
  APPEND RTAB1 TO ITAB1_TEMP .
  AT END OF NUM .
****相同的num作为一个组处理
    DESCRIBE TABLE ITAB1_TEMP LINES L_X .
    DO L_COUN TIMES .
      L_INDEX = L_INDEX + 1 .
******准备修改的表数据
      READ TABLE ITAB2 ASSIGNING INDEX L_INDEX .
      DO L_X TIMES .
********取得要修改的元素
        CONCATENATE '-' L_H INTO L_TEMP .
        ASSIGN (L_TEMP) TO .
        READ TABLE ITAB1_TEMP ASSIGNING INDEX L_V .
        CONCATENATE '-' L_CY INTO L_TEMP .
        ASSIGN (L_TEMP) TO .
********修改完成
        = .
********行列累加
        L_H = L_H + 1 .
        L_V = L_V + 1 .
      ENDDO .
******列累加,再初始化,继续处理下一列
      L_CY = L_CY + 1 .
      L_H = '0' .
      L_V = '1' .
    ENDDO .
****记得要清空临时表
    CLEAR: ITAB1_TEMP[],ITAB1_TEMP .
  ENDAT .
**重置列名
  L_CY = '0' .
ENDLOOP .
***上面转置好了,下面整理一下
**取得第一行的数据
READ TABLE ITAB2 ASSIGNING INDEX 1 .
LOOP AT ITAB2 INTO RTAB2 .
***如果下面有比第一行多的数据,把多的给第一行
  IF SY-TABIX <> 1 AND RTAB2-NUM = '' .
    IF >= RTAB2 .
      DELETE ITAB2 INDEX SY-TABIX .
    ELSE .
      = RTAB2 .
      DELETE ITAB2 INDEX SY-TABIX .
    ENDIF.
  ENDIF.
ENDLOOP .
***打印出来看看吧
LOOP AT ITAB2 INTO RTAB2 .
  WRITE: / RTAB2-NUM ,RTAB2-0 ,RTAB2-1,RTAB2-2 ,RTAB2-3,RTAB2-4,RTAB2-5,
           RTAB2-6,RTAB2-7,RTAB2-8,RTAB2-9 .
ENDLOOP .
===============================================================


你可能感兴趣的:(内表的行列转换.)