简体   繁体   English

表推导:将子集从内部表中提取到另一个表中

[英]Table comprehensions: get subset from internal table into another one

As stated in the topic, I want to have a conditioned subset of an internal table inside another internal table.如主题中所述,我希望在另一个内部表中包含一个内部表的条件子集。

Let us first look, what it may look like the old fashioned way.让我们先看看老式的方式会是什么样子。

DATA: lt_hugeresult TYPE tty_mytype,
      lt_reducedresult TYPE tty_mytype.  

SELECT "whatever" FROM "wherever" 
        INTO CORRESPONDING FIELDS OF TABLE lt_hugeresult 
          WHERE "any_wherecondition".
IF sy-subrc = 0.
  lt_reducedresult[] = lt_hugeresult[].
  DELETE lt_reducedresult WHERE col1 EQ 'a value'
                            AND col2 NE 'another value'
                            AND col3 EQ 'third value'.
 .
 .
 .


ENDIF.

We all may know this.我们都可能知道这一点。

Now I was reading about the table reducing stuff, which is introduced with abap 7.40, appearently SP8.现在我正在阅读有关减少表格的内容,它是在 abap 7.40 中引入的,似乎是 SP8。

Table Comprehensions – Building Tables Functionally表推导——以功能方式构建表

Table-driven:表驱动:

VALUE tabletype( FOR line IN tab WHERE ( … ) VALUE tabletype( FOR line IN tab WHERE ( … )

( … line-… … line-… … ) ) (……行-…………行-…………))

For each selected line in the source table(s), construct a line in the result table.对于源表中的每个选定行,在结果表中构造一行。 Generalization of value constructor from static to dynamic number of lines.值构造函数从静态行数到动态行数的泛化。

I was experimenting with that, but the results seem not really to fit, perhaps I am doing it wrong, or I might even need the condition-driven approach.我正在尝试这样做,但结果似乎不太合适,也许我做错了,或者我什至可能需要条件驱动的方法。

So, how would it look like, if I want to write the above statement with table comprehension techniques ?那么,如果我想用表格理解技术编写上述语句,它会是什么样子?

Until now I have this, delivering not that, what I need, and I have seen, that it seems, as if the "not equal" is not possible...直到现在我有了这个,而不是那个,我需要的东西,而且我已经看到,似乎“不等于”是不可能的......

DATA(reduced) =  VALUE tty_mytype( FOR checkline IN lt_hugeresult
                                   WHERE ( col1 = 'a value' )
                                         ( col2 = 'another value' )
                                         ( col3 = space )
                                 ).

Anyone having some hints ?有人有一些提示吗?

EDIT: Seems still not to work.编辑:似乎仍然不起作用。 Here is, as I do it:这是,正如我所做的:

Executable line:可执行线:

在此处输入图片说明

Debugger results:调试器结果:

在此处输入图片说明

Wrong Reduced:错误减少:

在此处输入图片说明

And what now ???现在怎么办 ???

You could use the FILTER operator with the EXCEPT WHERE addition to filter out any rows that match the where clause:您可以将FILTER运算符与EXCEPT WHERE添加一起使用来过滤掉与 where 子句匹配的任何行:

lt_reducedresult = FILTER # ( lt_hugeresult EXCEPT WHERE col1 = 'a value' 
                                                     AND col2 <> 'another value' 
                                                     AND col3 = 'a third value' ).

Note that lt_hugeresult would have to be a sorted table, and the col1 / col2 / col3 need to be key components (you can specify a secondary key using the USING KEY addition).请注意, lt_hugeresult必须是一个排序表,并且col1 / col2 / col3需要是关键组件(您可以使用USING KEY添加指定辅助键)。

The documentation for FILTER explicitly notes that: FILTER文档明确指出:

Table filtering can also be performed using a table comprehension or a table reduction with an iteration expression for table iterations with FOR.表过滤也可以使用表推导式或表约简来执行,其中带有 FOR 表迭代的迭代表达式。 The operator FILTER provides a shortened format for this special case and is more efficient to execute.运算符 FILTER 为这种特殊情况提供了一种缩短的格式,并且执行起来更有效率。

A table filter constructs the result row by row.表过滤器逐行构造结果。 If the result contains almost all rows in the source table, this method can be slower than copying the source table and deleting the surplus rows from the target table.如果结果几乎包含源表中的所有行,则此方法可能比复制源表并从目标表中删除多余的行要慢。

So your approach of using DELETE might actually be appropriate depending on the size of the table.因此,根据表的大小,您使用DELETE的方法实际上可能是合适的。

The Table Iterations may be a lot confusing when you use WHERE, because of parenthesis groups.由于括号组,当您使用 WHERE 时,表迭代可能会很混乱。

The "NOT EQUAL" condition is very well supported, as I show below in the solution of your first example. “NOT EQUAL”条件得到很好的支持,正如我在下面第一个示例的解决方案中所示。 The issue you observe is due to misproper use of parenthesis groups.您观察到的问题是由于括号组使用不当造成的。

You must absolutely define the whole logical expression after WHERE Inside ONE parenthesis group (one, or several elementary conditions separated by logical operators AND, OR, etc.)您必须在 WHERE Inside ONE 括号组之后绝对定义整个逻辑表达式(一个或多个由逻辑运算符 AND、OR 等分隔的基本条件)

After the parenthesis group for WHERE, you define usually only one parenthesis group which corresponds to the line to be added to the target internal table.在 WHERE 的括号组之后,通常只定义一个括号组,该括号组对应于要添加到目标内部表的行。 You may define subsequent parenthesis groups, if for each line in the source internal table, you want to add several lines in the target internal table.您可以定义后续的括号组,如果对于源内表中的每一行,您想在目标内表中添加几行。

In your example, only the first parenthesis group applies to WHERE (either col1 = 'a value' in your first example, or insplot = _ilnum in your second example).在您的示例中,只有第一个括号组适用于 WHERE(第一个示例中的 col1 = 'a value' 或第二个示例中的 insplot = _ilnum)。

The subsequent parenthesis groups correspond to the lines to be added, ie 2 lines are added for each source line in the first example (one line with col2 = 'another value', and one line with col3 = space), and 3 lines are added for each source line in the second example (one line with inspoper = i_evaluation-inspoper, one line with inspchar = i_evaluation-inspchar, one line corresponding to the line of _single_results).后面的括号组对应的是要添加的行,即第一个例子中每行添加2行(一行col2 = 'another value',一行col3 = space),加3行对于第二个示例中的每个源代码行(一行 inspoper = i_evaluation-inspoper,一行 inspchar = i_evaluation-inspchar,一行对应于 _single_results 行)。

So, you should write your code as follows.因此,您应该按如下方式编写代码。

First example :第一个例子:

DATA(reduced) =  VALUE tty_mytype( FOR checkline IN lt_hugeresult
                                   WHERE (     col1 =  'a value' 
                                           AND col2 <> 'another value'
                                           AND col3 =  'third value'
                                         )
                                   ( checkline )
                                 ).

Second example :第二个例子:

DATA(singres) = VALUE tbapi2045d4( FOR checkline IN _single_results
                                   WHERE (     insplot = _ilnum
                                           AND inspoper = i_evaluation-inspoper
                                           AND inspchar = i_evaluation-inspchar 
                                         )
                                   ( checkline )
                                 ).

I compared old-fashioned syntax of your above example with table comprehension technique and got exactly the same result.我将上述示例的老式语法与表格理解技术进行了比较,并得到了完全相同的结果。
Actually, your sample is not functional because it lacks row specification for constructed table reduced .实际上,您的示例不起作用,因为它缺少构造表的行规范reduced Try this one, which worked for me.试试这个,它对我有用。

DATA(reduced) =  VALUE tty_mytype( FOR checkline IN lt_hugeresult
                                WHERE ( col1 = 'a value' AND 
                                        col2 = 'another value' AND
                                        col3 = space )
                                   ( checkline )
                                 ).

In the above sample we have the most basic type of result row specification where is is absolutely similar to source table.在上面的示例中,我们有最基本的结果行规范类型,它与源表完全相似。 More sophisticated examples, where new table rows are evaluated with table iterations, can be found here .可以在此处找到更复杂的示例,其中使用表迭代评估新表行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM