[英]SUM through RBDRSEG lines in ABAP Query
I am having trouble figuring out where to start/how to get the correct output.我无法弄清楚从哪里开始/如何获得正确的输出。 I am very new to ABAP coding.
我对 ABAP 编码很陌生。 I am trying to create an Infoset query and need to do a bit of coding in SQ02.
我正在尝试创建一个 Infoset 查询,需要在 SQ02 中进行一些编码。
I have two tables joined - one being RBKP as the header for Invoice Receipts the other is RBDRSEG for the Invoice Document Items .我连接了两个表 - 一个是 RBKP 作为发票收据的标题,另一个是发票文档项目的RBDRSEG。
The query needs to run following some irrelevant parameters/variants, but when it does so it needs to - - -查询需要按照一些不相关的参数/变体运行,但是当它这样做时它需要 - - -
Look in RBDRSEG for all same Document numbers RBKP-BELNR EQ RBDRSEG-RBLNR在 RBDRSEG 中查找所有相同的文档编号 RBKP-BELNR EQ RBDRSEG-RBLNR
In doing so RBDRSEG may or may not have multiple line results for each Doc No.这样做时,RBDRSEG 可能会或可能不会为每个文档编号提供多行结果。
I need to total the field RBDRSEG-DMBTR for each Doc No. Result .我需要为每个 Doc No. Result 总计字段 RBDRSEG-DMBTR 。 (If there a 5 lines for a Doc. No.; DMBTR will have a different value for each that need to be totaled)
(如果文档编号有 5 行;DMBTR 将具有不同的值,需要总计)
At this point I need the output to only show (along with other fields in RBKP) One line with the SUM of the DMBTR field for each Doc.在这一点上,我需要输出只显示(以及 RBKP 中的其他字段)一行,其中包含每个 Doc 的 DMBTR 字段的总和。 No.
不。
I then need to have another field showing the difference of the Field RBKP - RMWWWR which is the Invoice Total and the Total that was calculated earlier for that Doc.然后,我需要有另一个字段显示字段 RBKP - RMWWWR 的差异,即发票总计和之前为该文档计算的总计。 No. for field DMBTR.
字段 DMBTR 的编号。
If you could help, I would be incredibly grateful.如果您能提供帮助,我将不胜感激。
first you need to define a structure that will contain your selection data.首先,您需要定义一个包含选择数据的结构。 An example structure for your requirement may look like this:
您的要求的示例结构可能如下所示:
don't forget to activate the structure and make sure it doesn't contain errors.不要忘记激活结构并确保它不包含错误。
now create the selection report.现在创建选择报告。 To use a report as the data selection method, you need to add two comments,
*<QUERY_HEAD>
and *<QUERY_BODY>
.要使用报表作为数据选择方法,您需要添加两个注释,
*<QUERY_HEAD>
和*<QUERY_BODY>
。 *<QUERY_HEAD>
has to be placed where your start-of-selecton usually would go, *<QUERY_BODY>
inside a loop that puts the selected lines into an internal table with the same name as the structure you defined in SE11. *<QUERY_HEAD>
必须放置在通常选择开始的位置, *<QUERY_BODY>
在一个循环中,该循环将所选行放入与您在 SE11 中定义的结构同名的内部表中。
I made an example report to show how this would work:我做了一个示例报告来展示这是如何工作的:
REPORT ZSTACK_RBKP_INFOSET_QUERY.
tables:
rbkp,
ZSTACK_RBKP_INFOSET_STR.
select-OPTIONS:
so_belnr for rbkp-belnr,
so_gjahr for rbkp-gjahr.
data:
itab type standard table of ZSTACK_RBKP_INFOSET_STR,
wa_itab type ZSTACK_RBKP_INFOSET_STR.
data:
lv_diff type dmbtr.
*here your selection starts.
*<QUERY_HEAD>
select rbkp~belnr
rbkp~gjahr
rbkp~rmwwr
rbkp~waers
sum( RBDRSEG~DMBTR ) as DMBTR
from RBKP left outer join RBDRSEG
on RBDRSEG~RBLNR eq RBKP~BELNR and
RBDRSEG~RJAHR eq RBKP~GJAHR
into corresponding fields of table itab
where rbkp~belnr in so_belnr and
rbkp~gjahr in so_gjahr
group by rbkp~belnr rbkp~gjahr rbkp~rmwwr rbkp~waers.
loop at itab into wa_itab.
lv_diff = wa_itab-dmbtr - wa_itab-rmwwr.
move lv_diff to wa_itab-diff.
modify itab from wa_itab.
endloop.
* this is the part that forwards your result set to the infoset
LOOP AT itab INTO ZSTACK_RBKP_INFOSET_STR.
*<QUERY_BODY>
ENDLOOP.
the sample report first selects the RBKP
lines along with a sum of RBDRSEG-DMBTR
for each document in RBKP
.样品报告首先选择
RBKP
线路用的总和沿着RBDRSEG-DMBTR
在每个文档RBKP
。 After that, a loop
updates the DIFF
column with the difference between the selected columns RMWWR
and DMBTR
.之后,
loop
使用所选列RMWWR
和DMBTR
之间的差异更新DIFF
列。
Unfortunately in our SAP System the table RBDRSEG
is empty, so I can't test that part of the report.不幸的是,在我们的 SAP 系统中,表
RBDRSEG
是空的,所以我无法测试报告的那部分。 But you can test the report in your system by just adding a break point before the first loop
and then start the report.但是您可以通过在第一个
loop
之前添加断点然后启动报告来测试系统中的报告。 You should then be able to have a look at the selected lines in internal table ITAB
and see if the selection works as expected.然后,您应该能够查看内部表
ITAB
中的选定行,并查看选择是否按预期工作。
caveats in the example report: both RBKP
and RBDRSEG
reference different currency fields.示例报告中的警告:
RBKP
和RBDRSEG
引用不同的货币字段。 So it may be possible your values in RMWWR
and DMBTR
are in different currencies ( RMWWR
is in document currency, DMBTR
seems to be in default company currency).因此,您在
RMWWR
和DMBTR
中的值可能是不同的货币( RMWWR
是文档货币, DMBTR
似乎是默认公司货币)。 If that can be the case, you will have to convert them into the appropriate currency before calculating the difference.如果是这种情况,您必须在计算差额之前将它们转换为适当的货币。 Please make sure to join
RBKP
and RBDRSEG
using both the document number in BELNR
/ RBLNR
and the year in GJAHR
/ RJAHR
(field in RBDRSEG
is RJAHR
, not GJAHR
, although GJAHR
also exists in RBDRSEG
).请务必加入
RBKP
和RBDRSEG
使用这两个文件号BELNR
/ RBLNR
在一年GJAHR
/ RJAHR
(现场RBDRSEG
是RJAHR
,不GJAHR
,虽然GJAHR
也存在RBDRSEG
)。
when your report works as expected, create the infoset based on your report.当您的报告按预期工作时,根据您的报告创建信息集。 You can then use the infoset like any other infoset.
然后,您可以像使用任何其他信息集一样使用信息集。
Update: I just realized that because you wrote about being new to ABAP I immediately assumed you need to create a report for your infoset.更新:我刚刚意识到,因为你写的是 ABAP 的新手,我立即认为你需要为你的信息集创建一个报告。 Depending on your actual requirements this may not be the case.
根据您的实际要求,情况可能并非如此。 You could create a simple infoset query over table RBKP and then use the infoset editor to add two more fields for the line total and the difference, then add some abap code that selects the sum of all corresponding lines in RBDRSEG and calculates the difference between RMWWR and that aggregated sum.
您可以在表 RBKP 上创建一个简单的信息集查询,然后使用信息集编辑器为行总计和差值添加两个字段,然后添加一些 abap 代码来选择 RBDRSEG 中所有相应行的总和并计算 RMWWR 之间的差值和那个总和。 This would probably be slower than a customized abap report as the select would have to be repeated for each line in RBKP, so it really depends on the amount of data your users are going to query.
这可能比自定义的 abap 报告慢,因为必须为 RBKP 中的每一行重复选择,因此这实际上取决于您的用户要查询的数据量。 A customized ABAP report is fine, flexible and quick but may be overkill and the number of people able to change a report is smaller than the number of people able to modify an infoset.
定制的 ABAP 报告很好、灵活且快速,但可能有点矫枉过正,而且能够更改报告的人数少于能够修改信息集的人数。
Additional Info on the variant using the infoset designer使用信息集设计器的关于变体的附加信息
first create a simple infoset reading only table RBKP (so no table join in the infoset definition).首先创建一个简单的信息集只读表 RBKP(因此信息集定义中没有表连接)。 Now go to application-specific enhancements:
现在转到特定于应用程序的增强功能:
In my example I already added 2 fields, LINETOTAL
and DIFFERENCE
.在我的示例中,我已经添加了 2 个字段,
LINETOTAL
和DIFFERENCE
。 Both have the same properties as RBDRSEG-DMBTR
.两者都具有与
RBDRSEG-DMBTR
相同的属性。 Make sure your field containing the sum of RBDRSEG-DMBTR
has a lower sequence (here '1') than the field containing the difference.确保包含
RBDRSEG-DMBTR
总和的RBDRSEG-DMBTR
序列(此处为“1”)低于包含差异的字段。 The sequence determines which fields will be calculated first.顺序决定了首先计算哪些字段。
Click on the coding button for the first field and add the coding to select the sum for a single RBKP
entry:单击第一个字段的编码按钮并添加编码以选择单个
RBKP
条目的总和:
Then do the same for the difference field:然后对差异字段执行相同操作:
Now you have both fields available in your field list, you can add them to your field group on the right:现在您的字段列表中有两个字段可用,您可以将它们添加到右侧的字段组中:
As mentioned before, the code you just entered will be processed for each line in RBKP.如前所述,RBKP 中的每一行都会处理您刚刚输入的代码。 So this might have a huge impact on runtime performance, depending on the size of your initial result set.
因此,这可能会对运行时性能产生巨大影响,具体取决于初始结果集的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.