Table 推导:从内部 table 获取子集到另一个子集
Table comprehensions: get subset from internal table into another one
正如主题中所述,我想要一个内部的条件子集
table里面另一个内部table.
让我们先看看,它可能看起来像老式的方式。
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.
我们可能都知道这一点。
现在我正在阅读有关 table reducing 的内容,它被引入
使用 abap 7.40,表面上是 SP8。
Table 理解 – 在功能上构建 Tables
Table驱动:
VALUE tabletype( FOR line IN tab WHERE ( … )
( … 行-… … 行-… … )
)
对于源 table(s) 中的每个选定行,在结果 table 中构造一行。值构造函数从静态到动态行数的泛化。
我正在对此进行试验,但结果似乎不太合适,
也许我做错了,或者我什至可能需要条件驱动的方法。
那么,如果我想用 table 理解技巧来写上面的语句,它会是什么样子?
直到现在我有这个,而不是那个,我需要的,我已经看到了,那个
好像,好像"not equal"是不可能的...
DATA(reduced) = VALUE tty_mytype( FOR checkline IN lt_hugeresult
WHERE ( col1 = 'a value' )
( col2 = 'another value' )
( col3 = space )
).
有人有一些提示吗?
编辑:似乎仍然无法正常工作。这是,正如我所做的那样:
执行table行:
调试器结果:
错误减少:
然后呢???
我将上面示例的老式语法与 table 理解技术进行了比较,得到了完全相同的结果。
实际上,您的示例不起作用,因为它缺少构造 table reduced
的行规范。
试试这个,对我有用。
DATA(reduced) = VALUE tty_mytype( FOR checkline IN lt_hugeresult
WHERE ( col1 = 'a value' AND
col2 = 'another value' AND
col3 = space )
( checkline )
).
在上面的示例中,我们有最基本的结果行规范类型,它与源 table 完全相似。 here.
可以找到更复杂的示例,其中新的 table 行使用 table 次迭代进行评估
您可以使用 FILTER
运算符和 EXCEPT WHERE
添加项来过滤掉与 where 子句匹配的任何行:
lt_reducedresult = FILTER # ( lt_hugeresult EXCEPT WHERE col1 = 'a value'
AND col2 <> 'another value'
AND col3 = 'a third value' ).
请注意,lt_hugeresult
必须是排序的 table,并且 col1
/col2
/col3
必须是关键组件(您可以使用 USING KEY
添加指定辅助键)。
documentation for FILTER 明确指出:
Table filtering can also be performed using a table comprehension or a table reduction with an iteration expression for table iterations with FOR. The operator FILTER provides a shortened format for this special case and is more efficient to execute.
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.
因此,根据 table 的大小,您使用 DELETE
的方法实际上可能是合适的。
当您使用 WHERE 时,Table 迭代可能会很混乱,因为括号组。
"NOT EQUAL" 条件得到很好的支持,如下面第一个示例的解决方案所示。您观察到的问题是由于括号组使用不当造成的。
必须在WHERE后绝对定义整个逻辑表达式在ONE括号组内(一个或几个基本条件,用逻辑运算符AND、OR等分隔)
在WHERE 的括号组之后,通常只定义一个括号组,它对应于要添加到目标内部的行table。您可以定义后续的括号组,如果对于源内部 table 中的每一行,您想要在目标内部 table.
中添加几行
在您的示例中,只有第一个括号组适用于 WHERE(第一个示例中的 col1 = 'a value',或者第二个示例中的 insplot = _ilnum)。
后面的括号组对应要添加的行,即在第一个示例中为每个源代码行添加2行(一行col2 = 'another value',一行col3 = space),第二个例子中每行源码增加3行(inspoper=[=33=一行,inspchar=[=34=一行],[=行对应一行35=]).
因此,您应该按如下方式编写代码。
第一个例子:
DATA(reduced) = VALUE tty_mytype( FOR checkline IN lt_hugeresult
WHERE ( col1 = 'a value'
AND col2 <> 'another value'
AND col3 = 'third value'
)
( checkline )
).
第二个例子:
DATA(singres) = VALUE tbapi2045d4( FOR checkline IN _single_results
WHERE ( insplot = _ilnum
AND inspoper = i_evaluation-inspoper
AND inspchar = i_evaluation-inspchar
)
( checkline )
).
正如主题中所述,我想要一个内部的条件子集 table里面另一个内部table.
让我们先看看,它可能看起来像老式的方式。
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.
我们可能都知道这一点。
现在我正在阅读有关 table reducing 的内容,它被引入 使用 abap 7.40,表面上是 SP8。
Table 理解 – 在功能上构建 Tables
Table驱动:
VALUE tabletype( FOR line IN tab WHERE ( … )
( … 行-… … 行-… … ) )
对于源 table(s) 中的每个选定行,在结果 table 中构造一行。值构造函数从静态到动态行数的泛化。
我正在对此进行试验,但结果似乎不太合适, 也许我做错了,或者我什至可能需要条件驱动的方法。
那么,如果我想用 table 理解技巧来写上面的语句,它会是什么样子?
直到现在我有这个,而不是那个,我需要的,我已经看到了,那个 好像,好像"not equal"是不可能的...
DATA(reduced) = VALUE tty_mytype( FOR checkline IN lt_hugeresult
WHERE ( col1 = 'a value' )
( col2 = 'another value' )
( col3 = space )
).
有人有一些提示吗?
编辑:似乎仍然无法正常工作。这是,正如我所做的那样:
执行table行:
调试器结果:
错误减少:
然后呢???
我将上面示例的老式语法与 table 理解技术进行了比较,得到了完全相同的结果。
实际上,您的示例不起作用,因为它缺少构造 table reduced
的行规范。
试试这个,对我有用。
DATA(reduced) = VALUE tty_mytype( FOR checkline IN lt_hugeresult
WHERE ( col1 = 'a value' AND
col2 = 'another value' AND
col3 = space )
( checkline )
).
在上面的示例中,我们有最基本的结果行规范类型,它与源 table 完全相似。 here.
可以找到更复杂的示例,其中新的 table 行使用 table 次迭代进行评估您可以使用 FILTER
运算符和 EXCEPT WHERE
添加项来过滤掉与 where 子句匹配的任何行:
lt_reducedresult = FILTER # ( lt_hugeresult EXCEPT WHERE col1 = 'a value'
AND col2 <> 'another value'
AND col3 = 'a third value' ).
请注意,lt_hugeresult
必须是排序的 table,并且 col1
/col2
/col3
必须是关键组件(您可以使用 USING KEY
添加指定辅助键)。
documentation for FILTER 明确指出:
Table filtering can also be performed using a table comprehension or a table reduction with an iteration expression for table iterations with FOR. The operator FILTER provides a shortened format for this special case and is more efficient to execute.
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.
因此,根据 table 的大小,您使用 DELETE
的方法实际上可能是合适的。
当您使用 WHERE 时,Table 迭代可能会很混乱,因为括号组。
"NOT EQUAL" 条件得到很好的支持,如下面第一个示例的解决方案所示。您观察到的问题是由于括号组使用不当造成的。
必须在WHERE后绝对定义整个逻辑表达式在ONE括号组内(一个或几个基本条件,用逻辑运算符AND、OR等分隔)
在WHERE 的括号组之后,通常只定义一个括号组,它对应于要添加到目标内部的行table。您可以定义后续的括号组,如果对于源内部 table 中的每一行,您想要在目标内部 table.
中添加几行在您的示例中,只有第一个括号组适用于 WHERE(第一个示例中的 col1 = 'a value',或者第二个示例中的 insplot = _ilnum)。
后面的括号组对应要添加的行,即在第一个示例中为每个源代码行添加2行(一行col2 = 'another value',一行col3 = space),第二个例子中每行源码增加3行(inspoper=[=33=一行,inspchar=[=34=一行],[=行对应一行35=]).
因此,您应该按如下方式编写代码。
第一个例子:
DATA(reduced) = VALUE tty_mytype( FOR checkline IN lt_hugeresult
WHERE ( col1 = 'a value'
AND col2 <> 'another value'
AND col3 = 'third value'
)
( checkline )
).
第二个例子:
DATA(singres) = VALUE tbapi2045d4( FOR checkline IN _single_results
WHERE ( insplot = _ilnum
AND inspoper = i_evaluation-inspoper
AND inspchar = i_evaluation-inspchar
)
( checkline )
).