<?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: [OpenSQL] Joining previously selected data in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540248#M1265326</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay so maybe ill guve more practical example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have many records in T558A table (over 10 milion) i need to :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. Fetch max date ENDDA for each employee&lt;/P&gt;&lt;P&gt;2. Join that data with one field of PA0003 table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So i do it in two queries :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT DISTINCT pernr FROM T558A GROUP BY pernr SORT BY endda DESCENDING.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the second one is SELECT SINGLE in a loop that fetches from PA0003.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The thing is that generating this query takes close to 10 min - im aiming on lowering the time as much as possible&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 19 Apr 2009 14:19:00 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2009-04-19T14:19:00Z</dc:date>
    <item>
      <title>[OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540242#M1265320</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;usually you do JOINS like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT foo1 foo2 INTO TABLE inttab FROM tab1 INNER JOIN ON tab1~field = tab2~field WHERE ....
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Its ok but the WHERE condition is applied on the whole set of data AFTER the join.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What i need to do is first select data THEN apply join on that selected data it would sound something like this :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT field1 INTO foo0 FROM (SELECT {here goes statement }) INNER JOIN ON.....&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I expect it to work in that manner that the inner SELECT is executed first - then the join is applied on the returned dataset.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is this possible to achieve in OpenSQL ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 18 Apr 2009 22:49:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540242#M1265320</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-18T22:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540243#M1265321</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;Use the For All Entries..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First select the data into internal table from itab1 and later select  data from itab2 using for all entries from itab1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Select * from &amp;lt;Table1&amp;gt; into  itab1 where &amp;lt;condition&amp;gt;.

Select * from &amp;lt;Table2&amp;gt; into  itab2 For All Entries in itab1 where  &amp;lt;field = itab1-field&amp;gt;.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT field1 INTO foo0 FROM &amp;lt;Table1&amp;gt; where Field = ( SELECT field From &amp;lt;Table2&amp;gt; Where &amp;lt;Condt&amp;gt;.).
ENDSELECT.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 01:47:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540243#M1265321</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-19T01:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540244#M1265322</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The best approach is to use select seperately.&lt;/P&gt;&lt;P&gt;Select the entries in the first table according to your condition .&lt;/P&gt;&lt;P&gt;In the second select you can use for all entries and fetch the data accordingly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Lalit Mohan Gupta.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 05:39:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540244#M1265322</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-19T05:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540245#M1265323</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jacek,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Select for all entries will not give you good performance. The best way to go is by using joins. If you are using the index correctly, your inner joins will give you much better performance than select for all entries or looping over internal tables. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But you can't use joins for cluster tables/pooled tables. In that case you will have to use for all entries. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The method you have mentioned above is possible in Native SQL. But here you can use sub queries in the where clause and not the from clause. Open SQL will not give you some of the flexibilities available with native SQL. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Abdullah&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 06:18:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540245#M1265323</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-19T06:18:08Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540246#M1265324</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Abdullah !&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the reply - but could you give me some native SQL Example that uses subqueries to achieve what i want ?&lt;/P&gt;&lt;P&gt;I just want to know ifi get things right - if i cant use subqueries in FROM clause - where should i use them ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 12:04:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540246#M1265324</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-19T12:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540247#M1265325</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jacek,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Have a look at these:&lt;/P&gt;&lt;P&gt;1) an example of &lt;STRONG&gt;nesting select queries&lt;/STRONG&gt; - not recommended as performance here is usually poor. Isntead use inner join which is more effective.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT foo1 foo2 FROM dbtab1 INTO wa_dbtab1.
    SELECT foo1 foo2 FROM dbtab2 INTO wa_dbtab2
                                    WHERE wa_dbtab1-field1 = wa_dbtab2-field1
                                            AND wa_dbtab1-field2 = wa_dbtab2-field2.
ENDSELECT.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;2) an example of &lt;STRONG&gt;subquery&lt;/STRONG&gt; - more effective than  above but generally not recommended too&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
SELECT foo1 foo2 INTO wa_dbtab1
                               FROM dbtab1
                                "subquery is formulated right here, this one will check read record from outer table only if there is corresponding entry in inner table 
                                WHERE EXISTS (SELECT foo1 FROM dbta2     "only a single column here allowed
                                                               WHERE .... ) 

ENDSELECT.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the latter example you can use as well &lt;STRONG&gt;LIKE&lt;/STRONG&gt;, &lt;STRONG&gt;=&lt;/STRONG&gt; , &lt;STRONG&gt;BETWEEN&lt;/STRONG&gt; , &lt;STRONG&gt;IN&lt;/STRONG&gt; after WHERE. Depending on the addition, your subquery can return either a single value or a set of values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For more information please refer [subqueries|http://help.sap.com/saphelp_nw70/helpdata/EN/dc/dc7614099b11d295320000e8353423/frameset.htm].&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note!&lt;/P&gt;&lt;P&gt;I am not talking about performance here. Generally fetching data into some internal tables can be more effective than the methods described above. Anyhow, that was not the question, right:)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Marcin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 13:53:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540247#M1265325</guid>
      <dc:creator>MarcinPciak</dc:creator>
      <dc:date>2009-04-19T13:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540248#M1265326</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay so maybe ill guve more practical example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have many records in T558A table (over 10 milion) i need to :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. Fetch max date ENDDA for each employee&lt;/P&gt;&lt;P&gt;2. Join that data with one field of PA0003 table.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So i do it in two queries :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT DISTINCT pernr FROM T558A GROUP BY pernr SORT BY endda DESCENDING.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the second one is SELECT SINGLE in a loop that fetches from PA0003.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The thing is that generating this query takes close to 10 min - im aiming on lowering the time as much as possible&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 14:19:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540248#M1265326</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-19T14:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540249#M1265327</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A JOIN is still probably your best bet. If you retrieve and process a lot of data, it will take some time. There's no getting around that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 20:28:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540249#M1265327</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-19T20:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540250#M1265328</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay - one last question&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does  join condition need to contain only '=' operator in SQL ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is this join valid in OpenSQL ? :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT f1 FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1~pernr = t2~pernr AND t1~date &amp;lt; t2~date WHERE......&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 21:19:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540250#M1265328</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2009-04-19T21:19:13Z</dc:date>
    </item>
    <item>
      <title>Re: [OpenSQL] Joining previously selected data</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540251#M1265329</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No, you can only use = when formulating a join condition. You can however set different comparison marks in WHERE clasue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please have a look at this query. It may help you with your requirement.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DATA: BEGIN OF wa,
       pernr TYPE pa0003-pernr,
       prdat TYPE pa0003-prdat,
       endda TYPE t558a-endda,
      END OF wa.

SELECT a~pernr MAX( a~endda ) b~prdat INTO (wa-pernr, wa-endda, wa-prdat)
                          FROM t558a AS a INNER JOIN pa0003 AS b ON
                               a~pernr = b~pernr
                          GROUP BY a~pernr b~prdat.
  WRITE: / wa-pernr, wa-endda, wa-prdat.
ENDSELECT.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One more thing. Don't use SELECT/SINGLE statements inside the loop. This produces same sturctured queries (even the values are different) hence DB interface sends alike queries to DB, which is unnecessary. Avoid this. Instead buffer such query in internal table, and once data fetched, read your table entires in a way you want. This will be relief for DB. &lt;/P&gt;&lt;P&gt;Of course this has to be considered case by case. Here, when data number is huge, this could cause unnecessary network load. So always try to think about those two aspects and choose the best fitting one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Marcin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 19 Apr 2009 21:54:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/opensql-joining-previously-selected-data/m-p/5540251#M1265329</guid>
      <dc:creator>MarcinPciak</dc:creator>
      <dc:date>2009-04-19T21:54:43Z</dc:date>
    </item>
  </channel>
</rss>

