<?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 Re: Select Query Fetching same records multiple time in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404748#M1645128</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Vishnu Tallapragada,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Its really helpful.. Thanks you so much.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 14 Jan 2012 11:09:21 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2012-01-14T11:09:21Z</dc:date>
    <item>
      <title>Select Query Fetching same records multiple time</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404743#M1645123</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi ABAPers,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have an issue where the select query is fetching the same records more than one time for particular sale order.&lt;/P&gt;&lt;P&gt;Kindly help to solve the issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the Select Query:-&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT  VBAK~VKORG VBAK~VTWEG VBAK~VBELN VBAK~KUNNR
               VBAK~ERDAT VBAK~LIFSK VBKD~INCO1 VBKD~INCO2
               VBAP~MATNR VBAP~ARKTX VBAP~MATKL VBAK~BSTNK
               VBAK~BSTDK VBAK~KNUMV VBAP~UEPOS VBAP~POSNR
               VBAP~KWMENG VBAK~VDATU VBAP~VRKME VBAK~ERNAM VBAK~WAERK 
  INTO CORRESPONDING FIELDS OF TABLE IT_PENDING FROM VBAK
  INNER JOIN VBAP ON VBAK~VBELN = VBAP~VBELN
  INNER JOIN VBKD ON VBAK~VBELN = VBKD~VBELN
  WHERE
  VBAK~VBELN IN SO_VBELN AND
  VBAK~ERDAT IN SO_ERDAT AND
  ( AUART EQ 'OR' OR AUART EQ 'ZEO' OR AUART EQ 'TA' OR AUART EQ 'LP' OR AUART EQ 'ZFOC' ) AND
  VKORG IN SO_VKORG AND
  VTWEG IN SO_VTWEG AND
  SPART IN SO_SPART AND
  KUNNR IN SO_KUNNR AND
  MATNR IN SO_MATNR AND
  MATKL IN SO_MATKL AND
  ABGRU EQ '' AND LIFSK &amp;lt;&amp;gt; '35'.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Suhas Saha on Jan 14, 2012 3:42 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 05:47:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404743#M1645123</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-14T05:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Select Query Fetching same records multiple time</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404744#M1645124</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Did you tried with select all entries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Madhu.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 07:49:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404744#M1645124</guid>
      <dc:creator>madhu_vadlamani</dc:creator>
      <dc:date>2012-01-14T07:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select Query Fetching same records multiple time</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404745#M1645125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You are getting duplicates because the second join with VBKD should be made with VBAP on both VBELN and POSNR. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since VBKD for most documents contains only header entry (VBKD-POSNR = '000000'), if we make an inner join, we will only select orders where VBKD has item level entries. So we need to do a left outer join between VBAP and VBKD so that the query will fetch all matching records from VBAP even if for some document VBKD contains only header entry.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So you need to change your query like below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It is a good practice to indent your queries so they are more legible. Also when you are using joins, it doesn't harm to qualify all fields with table~, so one clearly knows which field is from which table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT vbak~vkorg vbak~vtweg vbak~vbeln vbak~kunnr
       vbak~erdat vbak~lifsk vbkd~inco1 vbkd~inco2
       vbap~matnr vbap~arktx vbap~matkl vbak~bstnk
       vbak~bstdk vbak~knumv vbap~uepos vbap~posnr
       vbap~kwmeng vbak~vdatu vbap~vrkme vbak~ernam vbak~waerk
  INTO CORRESPONDING FIELDS OF TABLE it_pending
    FROM vbak INNER JOIN vbap
      ON vbak~vbeln = vbap~vbeln
              LEFT OUTER JOIN vbkd
      ON vbap~vbeln = vbkd~vbeln AND
         vbap~posnr = vbkd~posnr
        WHERE vbak~vbeln IN so_vbeln AND
              vbak~erdat IN so_erdat AND
            ( vbak~auart EQ 'OR' OR vbak~auart EQ 'ZEO' OR
              vbak~auart EQ 'TA' OR vbak~auart EQ 'LP' OR
              vbak~auart EQ 'ZFOC' ) AND
              vbak~vkorg IN so_vkorg AND
              vbak~vtweg IN so_vtweg AND
              vbak~spart IN so_spart AND
              vbak~kunnr IN so_kunnr AND
              vbap~matnr IN so_matnr AND
              vbap~matkl IN so_matkl AND
              vbap~abgru EQ '' AND 
              vbak~lifsk NE '35'.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PS: VBKD contains item level entries only when someone changes the VBKD business data fields for specific items in VA01/VA02 (I am assuming sales orders since you used order type OR). Otherwise all items default their values from header and VBKD contains a single entry where POSNR = '000000'.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 08:25:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404745#M1645125</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-14T08:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Select Query Fetching same records multiple time</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404746#M1645126</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Vishnu Tallapragada,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks a lot for your help..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now Query is working good.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 08:47:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404746#M1645126</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-14T08:47:25Z</dc:date>
    </item>
    <item>
      <title>Re: Select Query Fetching same records multiple time</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404747#M1645127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Suhale,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;On retrospection, I found the query in my above response STILL HAS ONE ISSUE. When we are making a left outer join on vbkd&lt;SUB&gt;posnr = vbap&lt;/SUB&gt;posnr we are only fetching vbkd&lt;SUB&gt;inco1 and vbkd&lt;/SUB&gt;inco2 values where item level entries exist in VBKD which match with vbkd&lt;SUB&gt;posnr = vbap&lt;/SUB&gt;posnr. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Actually we should bring inco1 and inco2 from vbkd when a matching entry exists in vbkd where vbkd&lt;SUB&gt;posnr = vbap&lt;/SUB&gt;posnr and if there is no entry matching vbkd&lt;SUB&gt;posnr = vbap&lt;/SUB&gt;posnr, then we should fetch inc01 and inco2 from VBKD where posnr = '000000' (i.e., from header).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is how SAP behaves when you open an item in VA03 and look at inco1 and inco2. They come from the item if an entry corresponding to that item exists in VBKD otherwise they come from VBKD header record (where vbkd~posnr = '000000')&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We can't use VBKD in the joins and still be able to do the above. So we need to fetch inco1, inco2 from VBKD separately like below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;FIELD-SYMBOLS: &amp;lt;fs_pending&amp;gt; LIKE LINE OF it_pending.

SELECT vbak~vkorg vbak~vtweg vbak~vbeln vbak~kunnr
       vbak~erdat vbak~lifsk "vbkd~inco1 vbkd~inco2
       vbap~matnr vbap~arktx vbap~matkl vbak~bstnk
       vbak~bstdk vbak~knumv vbap~uepos vbap~posnr
       vbap~kwmeng vbak~vdatu vbap~vrkme vbak~ernam vbak~waerk
  INTO CORRESPONDING FIELDS OF TABLE it_pending
    FROM vbak INNER JOIN vbap
      ON vbak~vbeln = vbap~vbeln
        WHERE vbak~vbeln IN so_vbeln AND
              vbak~erdat IN so_erdat AND
            ( vbak~auart EQ 'OR' OR vbak~auart EQ 'ZEO' OR
              vbak~auart EQ 'TA' OR vbak~auart EQ 'LP' OR
              vbak~auart EQ 'ZFOC' ) AND
              vbak~vkorg IN so_vkorg AND
              vbak~vtweg IN so_vtweg AND
              vbak~spart IN so_spart AND
              vbak~kunnr IN so_kunnr AND
              vbap~matnr IN so_matnr AND
              vbap~matkl IN so_matkl AND
              vbap~abgru EQ '' AND
              vbak~lifsk NE '35'.

  LOOP AT it_pending ASSIGNING &amp;lt;fs_pending&amp;gt;.
    SELECT SINGLE inco1 inco2
      FROM vbkd
        INTO (&amp;lt;fs_pending&amp;gt;-inco1,&amp;lt;fs_pending&amp;gt;-inco2)
          WHERE vbkd~vbeln = &amp;lt;fs_pending&amp;gt;-vbeln AND
                vbkd~posnr = &amp;lt;fs_pending&amp;gt;-posnr.
    IF sy-subrc NE 0.
      SELECT SINGLE inco1 inco2
        FROM vbkd
          INTO (&amp;lt;fs_pending&amp;gt;-inco1, &amp;lt;fs_pending&amp;gt;-inco2)
            WHERE vbkd~vbeln = &amp;lt;fs_pending&amp;gt;-vbeln AND
                  vbkd~posnr = '000000'.
    ENDIF.
  ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Vishnu Tallapragada.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 08:59:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404747#M1645127</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-14T08:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Select Query Fetching same records multiple time</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404748#M1645128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Vishnu Tallapragada,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Its really helpful.. Thanks you so much.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 11:09:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-query-fetching-same-records-multiple-time/m-p/8404748#M1645128</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-01-14T11:09:21Z</dc:date>
    </item>
  </channel>
</rss>

