<?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 Subquery in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699843#M1104468</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;performance wise my advice is the best (I am 99.99% sure...), if you really want to go for subquery, pls. have a look at the following sample:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DATA : lt_objid TYPE TABLE OF hrp1000.
DATA : lw_objid TYPE hrp1000.

SELECT objid MIN( begda ) FROM hrp1000
INTO CORRESPONDING FIELDS OF lw_objid
WHERE otype = 'S'
AND    objid  IN ra_objid     "the data range requested from BW
AND    plvar   = '01'
AND    istat   = '1'
GROUP BY objid.
  SELECT MAX( endda )
  INTO lw_objid-endda
  FROM hrp1000
  WHERE objid EQ lw_objid-objid.
  APPEND lw_objid TO lt_objid.
ENDSELECT.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 29 Oct 2008 08:47:46 GMT</pubDate>
    <dc:creator>JozsefSzikszai</dc:creator>
    <dc:date>2008-10-29T08:47:46Z</dc:date>
    <item>
      <title>Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699836#M1104461</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;I have the following problem, can anyone help?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Data in HRP1000

Objid             Begda                 Endda
1234              01.01.1900          31.12.2006
1234              01.01.2007          31.12.2007
1234              01.01.2008          31.12.2010
5678              01.01.2007          30.06.2007
5678              01.07.2007          31.07.2007

Required in my internal table in 1 Select statement i.e. 
without manipulating data after Select statement

Objid             Begda                 Endda
1234              01.01.1900          31.12.2010
5678              01.01.2007          31.07.2007
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;             &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In other words, in a single select statement, I want to select the DISTINCT entries from HRP1000 with MIN begda and MAX endda where multiple records exist without manipulating the data in a loop after the select statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;lt;removed_by_moderator&amp;gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Sougata.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Julius Bussche on Oct 29, 2008 10:44 AM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 06:31:50 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699836#M1104461</guid>
      <dc:creator>Sougata</dc:creator>
      <dc:date>2008-10-29T06:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699837#M1104462</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;after selecting data into internal table, sort your internal table descending using pernr,begda,endda and then use this statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DELETE ADJACENT DUPLICATES FROM it_pa0001 COMPARING pernr.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 06:37:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699837#M1104462</guid>
      <dc:creator>GauthamV</dc:creator>
      <dc:date>2008-10-29T06:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699838#M1104463</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;First Select Distinct object id into one internal table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then using For all entries select min and max separately into another internal table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I dont think this can be done with a singe select statement as you are taking min and max values from different records.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Karthik D&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 07:08:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699838#M1104463</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-10-29T07:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699839#M1104464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;try this code....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
sort itab1 by objid.
       loop at &amp;lt;itab1&amp;gt;.
         at new objid.
              &amp;lt;append into new itab2&amp;gt;.
         endat.
       endloop.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Arunima&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 07:21:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699839#M1104464</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-10-29T07:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699840#M1104465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Chatterji,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i think the data what you are getting is wrong, the enddate for the 3rd and 5th records will be 31.12.9999.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then you can write &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In select statement write &lt;STRONG&gt;ENDDA =&amp;lt; SY-DATUM&lt;/STRONG&gt;. and&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;BEGDA =&amp;gt; SY-DATUM&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;i.e the enddate should be equal or greter than present date.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if your data is correct then , it is difficult to get the required data in one single select statement.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;kumar M.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 07:30:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699840#M1104465</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-10-29T07:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699841#M1104466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would do the following (pseudo code!):&lt;/P&gt;&lt;P&gt;Two internal tables will be necessary (both look the same)&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT ...
FROM hrp1000
INTO TABLE itab1
WHERE ...

LOOP AT itab1.
READ TABLE itab2 WITH KEY objid = itab1-objid. "Check if entry is already in itab2
IF sy-subrc EQ 0. "If yes
IF itab1-begda LT itab2-begda. "Check for lower begda
itab2-begda = itab1-begda.
ENDIF
IF itab1-ennda GT itab2-endda. "Check for higher ennda
itab2-endda = itab1-endda.
ENDIF.
MODIFY itab2.
ELSE. "If not ==&amp;gt; insert
INSERT itab1 INTO itab2.
ENDIF.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 07:47:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699841#M1104466</guid>
      <dc:creator>JozsefSzikszai</dc:creator>
      <dc:date>2008-10-29T07:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699842#M1104467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for trying to help but can I ask you not to suggest anything else &lt;/P&gt;&lt;P&gt;other than what I've asked in the first place i.e. the logic within one SELECT &lt;/P&gt;&lt;P&gt;statement. Its easy to get what I require with the help of 2 internal tables via &lt;/P&gt;&lt;P&gt;table manipulation after selection of data from database etc. but I don't want &lt;/P&gt;&lt;P&gt;to do that....because this is part of a extractor logic for a BW generic DataSource &lt;/P&gt;&lt;P&gt;to load a particular InfoObject - as a result run-time is the most important criteria&lt;/P&gt;&lt;P&gt;that one has to bear in mind. I cannot possibly use multiple internal tables then &lt;/P&gt;&lt;P&gt;use LOOP, READ, SORT, DELETE etc....that's not an option here. I could have &lt;/P&gt;&lt;P&gt;used PROVIDE statement to get what I want but that is also expensive in &lt;/P&gt;&lt;P&gt;regards to performance.....so even that is not an option!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was thinking in the lines of a subquery within a SELECT statement....but &lt;/P&gt;&lt;P&gt;couldn't get the results I'm looking for, so I was wondering if anyone has done&lt;/P&gt;&lt;P&gt;anything similar before?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
Data: lt_objid type standard table of zhr_objid_range.   "structure is objid, begda, endda

Select distinct objid begda endda from hrp1000
into corresponding fields of table lt_objid
where otype = 'S'
and    objid  in ra_objid     "the data range requested from BW
and    plvar   = '01'
and    istat   = '1'
and    begda = ( select min( begda ) from hrp1000  "not sure about this part...
                        where otype = 'S'
                        and    objid  in ra_objid
                        and    plvar = '01'
                        and    istat = '1'
                        and    begda = ??   "not sure about the subquery here
                        and    endda = ?? ) "not sure about the subquery here
and    endda = ( select max( endda ) from hrp1000 "not sure about this part....
                        where otype = 'S'
                        and    objid  in ra_objid
                        and    plvar = '01'
                        and    istat = '1'
                        and    begda = ??     "not sure about the subquery here
                        and    endda = ?? ).  "not sure about the subquery here
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Sougata.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Sougata Chatterjee on Oct 29, 2008 7:46 PM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 08:37:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699842#M1104467</guid>
      <dc:creator>Sougata</dc:creator>
      <dc:date>2008-10-29T08:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: Select Subquery</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699843#M1104468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;performance wise my advice is the best (I am 99.99% sure...), if you really want to go for subquery, pls. have a look at the following sample:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DATA : lt_objid TYPE TABLE OF hrp1000.
DATA : lw_objid TYPE hrp1000.

SELECT objid MIN( begda ) FROM hrp1000
INTO CORRESPONDING FIELDS OF lw_objid
WHERE otype = 'S'
AND    objid  IN ra_objid     "the data range requested from BW
AND    plvar   = '01'
AND    istat   = '1'
GROUP BY objid.
  SELECT MAX( endda )
  INTO lw_objid-endda
  FROM hrp1000
  WHERE objid EQ lw_objid-objid.
  APPEND lw_objid TO lt_objid.
ENDSELECT.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Oct 2008 08:47:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/select-subquery/m-p/4699843#M1104468</guid>
      <dc:creator>JozsefSzikszai</dc:creator>
      <dc:date>2008-10-29T08:47:46Z</dc:date>
    </item>
  </channel>
</rss>

