<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic One Select query from two itabs in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157621#M1977606</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
  &lt;P&gt; I have two internal tables : itab1 holding BKPF data &amp;amp; itab2 holding RSEG data.&lt;/P&gt;
  &lt;P&gt;BELNR is the common field on both of them.&lt;/P&gt;
  &lt;P&gt;Now i need to get data from BSEG using one select query based on BELNR'S from itab1 &amp;amp; itab2.&lt;/P&gt;
  &lt;P&gt;How do i do that?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Jun 2020 05:00:36 GMT</pubDate>
    <dc:creator>ricky_shaw</dc:creator>
    <dc:date>2020-06-16T05:00:36Z</dc:date>
    <item>
      <title>One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157621#M1977606</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
  &lt;P&gt; I have two internal tables : itab1 holding BKPF data &amp;amp; itab2 holding RSEG data.&lt;/P&gt;
  &lt;P&gt;BELNR is the common field on both of them.&lt;/P&gt;
  &lt;P&gt;Now i need to get data from BSEG using one select query based on BELNR'S from itab1 &amp;amp; itab2.&lt;/P&gt;
  &lt;P&gt;How do i do that?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 05:00:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157621#M1977606</guid>
      <dc:creator>ricky_shaw</dc:creator>
      <dc:date>2020-06-16T05:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157622#M1977607</link>
      <description>&lt;P&gt;try,&lt;/P&gt;&lt;P&gt;Both internal  table contains same structure means   use append  lines of   it_rseg to it_bkpf or&lt;/P&gt;&lt;P&gt;Select  * from bseg into table it_bseg for all entries in  it_bkpf&lt;/P&gt;&lt;P&gt;Select * from bseg appending table it_bseg for all entries in it_rseg.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;RAmesh&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 05:06:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157622#M1977607</guid>
      <dc:creator>VenkatRamesh_V</dc:creator>
      <dc:date>2020-06-16T05:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157623#M1977608</link>
      <description>&lt;P&gt;&lt;SPAN class="mention-scrubbed"&gt;ricky.shaw&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;You could try with the below code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SORT: IT_BKPF, IT_RSEG.
* Also Try Deleting Adjacent Duplicates from the above two tables after sorting by comparing the required fields

IF it_bkpf[] IS NOT INITIAL.
  SELECT *
  FROM bseg
  INTO TABLE @DATA(it_bseg)
  FOR ALL ENTRIES IN @it_bkpf
  WHERE belnr = @it_bkpf-belnr and
        BUKRS = @it_bkpf-bukrs and  * Hope the field is in it_bkpf
        GJAHR = @it_bkbf-bukrs.     * Hope the field is in it_bkpf
  IF sy-subrc EQ 0.
    SORT it_bseg BY belnr.
  ENDIF.
ENDIF.

IF it_rseg[] IS NOT INITIAL.
  SELECT *
  FROM bseg
  APPENDING TABLE @it_bseg
  FOR ALL ENTRIES IN @it_rseg
  WHERE belnr = @it_rseg-belnr and
        BUKRS = @it_rseg-bukrs and  * Hope the field is in it_rseg
        GJAHR = @it_rseg-bukrs.     * Hope the field is in it_rseg
  IF sy-subrc EQ 0.
    SORT it_bseg BY belnr.
  ENDIF.
ENDIF.&amp;lt;br&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Regards!&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 05:47:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157623#M1977608</guid>
      <dc:creator>former_member1716</dc:creator>
      <dc:date>2020-06-16T05:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157624#M1977609</link>
      <description>&lt;P&gt;Just don't forget to check if IT_BKPF and IT_RSEG have records. Otherwise this will return whole BSEG table.&lt;/P&gt;&lt;P&gt;As a rule of thumb, always check for records in table that is used in FOR ALL ENTRIES.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;IF it_bkpf[] IS NOT INITIAL.
  ...
ENDIF.

IF it_rseg[] IS NOT INITIAL.
  ..
ENDIF.&lt;/CODE&gt;&lt;/PRE&gt;&lt;BR /&gt;Kind regards,&lt;BR /&gt;Mateusz</description>
      <pubDate>Tue, 16 Jun 2020 06:35:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157624#M1977609</guid>
      <dc:creator>MateuszAdamus</dc:creator>
      <dc:date>2020-06-16T06:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157625#M1977610</link>
      <description>&lt;SPAN class="mention-scrubbed"&gt;mateuszadamus&lt;/SPAN&gt;,Code Corrected! Thanks!</description>
      <pubDate>Tue, 16 Jun 2020 06:51:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157625#M1977610</guid>
      <dc:creator>former_member1716</dc:creator>
      <dc:date>2020-06-16T06:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157626#M1977611</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You can go for union as well if result structure is same.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Girdhari&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 06:55:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157626#M1977611</guid>
      <dc:creator>former_member16553</dc:creator>
      <dc:date>2020-06-16T06:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157627#M1977612</link>
      <description>&lt;SPAN class="mention-scrubbed"&gt;girdhari.mondal&lt;/SPAN&gt;,Yes, but that has a dependency on the GUI version and database in use.</description>
      <pubDate>Tue, 16 Jun 2020 07:03:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157627#M1977612</guid>
      <dc:creator>former_member1716</dc:creator>
      <dc:date>2020-06-16T07:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157628#M1977613</link>
      <description>&lt;P&gt;Hi  &lt;SPAN class="mention-scrubbed"&gt;satishkumarbalasubramanian&lt;/SPAN&gt;,&lt;BR /&gt;Please note that BELNR is not the full key of BSEG table and BELNR is not unique number thus please also include field GJAHR and BUKRS in your code sample - otherwise we will be answering questions like "why the code is returning incorrect documents". &lt;BR /&gt;Regards,&lt;BR /&gt;Bartosz &lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 07:04:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157628#M1977613</guid>
      <dc:creator>ziolkowskib</dc:creator>
      <dc:date>2020-06-16T07:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157629#M1977614</link>
      <description>&lt;P&gt;Hi  &lt;SPAN class="mention-scrubbed"&gt;ricky.shaw&lt;/SPAN&gt;,&lt;BR /&gt;If you wanted to have it in one SELECT statement I would suggest creating a temporary table (i.e. LT_BSEG_PARTIAL_KEY) consisting of key fields (BELNR, GJAHR, BUKRS) and populate it based on entries you earlier fetched from BKPF and RSEG. I would also suggest sorting that table and deleting adjacent duplicates from the table. And as mentioned by &lt;SPAN class="mention-scrubbed"&gt;mateuszadamus&lt;/SPAN&gt; please &lt;STRONG&gt;always check if your table is not empty before using it with FOR ALL ENTRIES statement&lt;/STRONG&gt;. &lt;BR /&gt;Regards,&lt;BR /&gt;Bartosz &lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 07:10:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157629#M1977614</guid>
      <dc:creator>ziolkowskib</dc:creator>
      <dc:date>2020-06-16T07:10:01Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157630#M1977615</link>
      <description>&lt;SPAN class="mention-scrubbed"&gt;ziolkowskib&lt;/SPAN&gt;,Yes you are right, Added the code but it is mentioned in the question only BELNR is the common field.</description>
      <pubDate>Tue, 16 Jun 2020 07:15:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157630#M1977615</guid>
      <dc:creator>former_member1716</dc:creator>
      <dc:date>2020-06-16T07:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157631#M1977616</link>
      <description>&lt;P&gt;Do you run S/4HANA or an old version of SAP ERP? &lt;STRONG&gt;BSEG &lt;/STRONG&gt;is a transparent table in S/4HANA but a clustered table in older versions, that will do two completely different answers. (and eventually tell us your ABAP version so that to get an even more precise answer)&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 09:54:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157631#M1977616</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2020-06-16T09:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: One Select query from two itabs</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157632#M1977617</link>
      <description>&lt;P&gt;As mentioned by Bartosz, the access to BSEG cannot be done based on BELNR only, it must be done based on the three primary key columns BELNR, GJAHR, BUKRS, otherwise you may retrieve several non-related lines from BSEG.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jun 2020 10:47:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/one-select-query-from-two-itabs/m-p/12157632#M1977617</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2020-06-16T10:47:35Z</dc:date>
    </item>
  </channel>
</rss>

