如何将内部表格行转换为列? [英] How to transpose an internal table rows into columns?

查看:24
本文介绍了如何将内部表格行转换为列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的内部表行转换为列,我想修复第一列,我正在尝试使用以下代码进行操作,但没有得到预期的结果......它没有转换所有的行成列

I want to transpose my internal table rows into column and i want to fix the first column,i am trying to do it with the following code but i am not getting the expected result....it is not converting all the rows into columns

*Types Declaration
Types: BEGIN OF ty_t001w,
         ekorg TYPE t001w-ekorg,
         werks TYPE t001w-werks,
         name1 TYPE t001w-name1,
       END OF ty_t001w.

**Field Symbols Declaration
 FIELD-SYMBOLS:  <fs1> TYPE any,
                 <fs2> TYPE any.

**Internal table and work area declaration
 DATA: it1_col_row TYPE STANDARD TABLE OF ty_t001w,
       wa1_col_row TYPE ty_t001w,
       it2_col_row TYPE STANDARD TABLE OF ty_t001w,
       wa2_col_row TYPE ty_t001w,
       cline   TYPE sy-tabix.

**Filling internal table with data

Select *
  from t001w into corresponding fields of table it1_col_row
  where ekorg = p_ekorg
  and fabkl = p_fabkl.

**Looping Internal table to display data
 LOOP AT it1_col_row INTO wa1_col_row.
   WRITE: / wa1_col_row-ekorg, wa1_col_row-werks,wa1_col_row-name1.
 ENDLOOP.
 WRITE: /.

**Looping internal table to change rows into columns
 LOOP AT it1_col_row INTO wa1_col_row.
   CLEAR wa2_col_row.
   ASSIGN COMPONENT sy-tabix OF STRUCTURE wa2_col_row TO <fs1>.
   cline = sy-tabix.
   DO.
     ASSIGN COMPONENT sy-index OF STRUCTURE wa1_col_row TO <fs2>.
     IF sy-subrc NE 0.
       EXIT.
     ENDIF.
     IF cline = 1.
       <fs1> = <fs2>.
       APPEND wa2_col_row TO it2_col_row.
     ELSE.
       READ TABLE it2_col_row INTO wa2_col_row INDEX sy-index.
       <fs1> = <fs2>.
       MODIFY it2_col_row FROM wa2_col_row INDEX sy-index.
     ENDIF.
   ENDDO.
 ENDLOOP.
*
**Looping internal table to display
 LOOP AT it2_col_row INTO wa2_col_row.
   WRITE: / wa2_col_row-ekorg,wa2_col_row-werks, wa2_col_row-name1.
 ENDLOOP.

推荐答案

注意你的 ty_t001w 的字段类型有不同的长度:

Notice that the field types of your ty_t001w have different length:

  • ekorg TYPE t001w-ekorgCHAR 4
  • werks TYPE t001w-werks 也有 CHAR 4,但是
  • name1 TYPE t001w-name1CHAR 30
  • ekorg TYPE t001w-ekorg has CHAR 4
  • werks TYPE t001w-werks has also CHAR 4, but
  • name1 TYPE t001w-name1 has CHAR 30

您对源表 (it1_col_row) 和目标表 (it2_col_row) 使用相同类型的 ty_t001w.因此,当您将源行表映射到目标列表时,将 30 个字符的字段 name1 映射到 4 个字符的字段 ekorg.当我在我的系统中执行你的程序时,我得到了以下输出(取决于我的数据库表 t001w 的内容):

You are using this same type ty_t001w for your source table (it1_col_row) as well as your target table (it2_col_row). So when you are mapping your source rows table to the target columns table then the 30 character field name1 is mapped to the 4 character field ekorg. When I executed your program in my system I had the following output (dependent on the contents of my DB table t001w):

0001 0001 Werk 0001
0001 0002 Werk 0002
0001 0003 Werk 0003
0001 RAD1 Werk RAD1

0001 0001 0001
0001 0002 RAD1
Werk Werk Werk RAD1

乍一看,这看起来像是它没有将所有行都转换为列".但是在调试器中我注意到Werk 0001"实际上是一个值,而不是两个!但是,该值被截断为仅Werk",因为它是从 30 个字符的字段映射到 4 个字符的字段.这发生在第 1 列(Werk 0002")和第 2 列(Werk 0003")的底部值上.第 3 列的底部值(Werk RAD1")被正确映射,因为这里它是从 30 个字符的字段映射到 30 个字符的字段.

At first glance this looks like "it is not converting all the rows into columns". But in the debugger I noticed that "Werk 0001" is actually one value, not two! However the value is truncated to only "Werk" because it is mapped from a 30 character field to the 4 character field. This happens to the bottom value of column 1 ("Werk 0002") and 2 ("Werk 0003"). The bottom value of column 3 ("Werk RAD1") is mapped correctly because here it's mapped from the 30 character field to the 30 character field.

为了解决这个问题,我为目标表 it2_col_row 创建了一个额外的 TYPES 定义 ty_t001w_col.在此 TYPE 中,所有字段的最大长度为 30 个字符,以确保不会发生截断(请参阅下面的 abap 代码).它生成以下输出:

To correct this issue I have created an extra TYPES definition ty_t001w_col for the target table it2_col_row. In this TYPE all fields have the maximum length of 30 characters ensuring no truncation may occur (see abap code below). It generates the following output:

0001 0001 Werk 0001
0001 0002 Werk 0002
0001 0003 Werk 0003
0001 RAD1 Werk RAD1

0001                           0001                           0001
0001                           0002                           RAD1
Werk 0001                      Werk 0002                      Werk RAD1

更正后的报告:

REPORT zhd_stackoverflow_q27163908.

PERFORM function
      USING '0001'
            '01'.

FORM function
  USING p_ekorg TYPE ekorg
        p_fabkl TYPE fabkl.

Types Declaration
TYPES: BEGIN OF ty_t001w,
         ekorg TYPE t001w-ekorg,
         werks TYPE t001w-werks,
         name1 TYPE t001w-name1,
       END OF ty_t001w.
TYPES: BEGIN OF ty_t001w_col,
         ekorg TYPE t001w-name1,
         werks TYPE t001w-name1,
         name1 TYPE t001w-name1,
       END OF ty_t001w_col.

*Field Symbols Declaration
 FIELD-SYMBOLS:  <fs1> TYPE any,
                 <fs2> TYPE any.

*Internal table and work area declaration
 DATA: it1_col_row TYPE STANDARD TABLE OF ty_t001w,
       wa1_col_row TYPE ty_t001w,
       it2_col_row TYPE STANDARD TABLE OF ty_t001w_col,
       wa2_col_row TYPE ty_t001w_col,
       cline   TYPE sy-tabix.

*Filling internal table with data

SELECT *
  FROM t001w INTO CORRESPONDING FIELDS OF TABLE it1_col_row
  WHERE ekorg = p_ekorg
  AND fabkl = p_fabkl.

*Looping Internal table to display data
 LOOP AT it1_col_row INTO wa1_col_row.
   WRITE: / wa1_col_row-ekorg, wa1_col_row-werks,wa1_col_row-name1.
 ENDLOOP.
 WRITE: /.

*Looping internal table to change rows into columns
 LOOP AT it1_col_row INTO wa1_col_row.
   CLEAR wa2_col_row.
   ASSIGN COMPONENT sy-tabix OF STRUCTURE wa2_col_row TO <fs1>.
   cline = sy-tabix.
   DO.
     ASSIGN COMPONENT sy-index OF STRUCTURE wa1_col_row TO <fs2>.
     IF sy-subrc NE 0.
       EXIT.
     ENDIF.
     IF cline = 1.
       <fs1> = <fs2>.
       APPEND wa2_col_row TO it2_col_row.
     ELSE.
       READ TABLE it2_col_row INTO wa2_col_row INDEX sy-index.
       <fs1> = <fs2>.
       MODIFY it2_col_row FROM wa2_col_row INDEX sy-index.
     ENDIF.
   ENDDO.
 ENDLOOP.

*Looping internal table to display
 LOOP AT it2_col_row INTO wa2_col_row.
   WRITE: / wa2_col_row-ekorg,wa2_col_row-werks, wa2_col_row-name1.
 ENDLOOP.

 ENDFORM.

这篇关于如何将内部表格行转换为列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆