<?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: SQL SELECT in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240286#M141891</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Denise,&lt;/P&gt;&lt;P&gt;It wouldn't work if you have more than one column to select.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can add one more select query to fetch the rows for the corresponding maximum values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;TABLES: ztest.
DATA itab LIKE TABLE OF ztest WITH HEADER LINE.
DATA itab2 LIKE TABLE OF ztest WITH HEADER LINE.

SELECT ordernum max( version )
  FROM ztest
  INTO TABLE itab
  GROUP BY ordernum.

SELECT ordernum version status
  FROM ztest
  INTO TABLE itab2
  FOR ALL ENTRIES IN itab
  WHERE ordernum = itab-ordernum
    AND version  = itab-version.

LOOP AT itab2.
  WRITE: / itab2-ordernum, itab2-version, itab2-status.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Wenceslaus.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 13 Apr 2006 02:43:31 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2006-04-13T02:43:31Z</dc:date>
    <item>
      <title>SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240277#M141882</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to do a sql select like "SELECT TOP 1 FROM..."&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My table has different versions of orders.  The order# may be 12345 and there may be 5 versions - 1 through 5.  If I want to select a lot of orders, and only select the latest version, I want to select the latest version.   For some orders, the latest version may be 3, for others it may be 2, etc.  How can I construct a SQL select in ABAP to select only the latest version for each order (highest version number)?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Apr 2006 22:11:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240277#M141882</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-11T22:11:52Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240278#M141883</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You will have to get all of them to start.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Select * into table itab
       from ztable 
             where.....&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then sort the itab by order number and version descending.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

sort itab by vbeln ascending
             version descending.



&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now delete the adjacent duplicates.  This should delete all others after the first one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

delete adjacent duplicates from itab
               comparing vbeln.

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Apr 2006 22:32:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240278#M141883</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2006-04-11T22:32:27Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240279#M141884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I did that.  The issue is, this table is very large.  I am using this in a Netweaver BSP application, and we are having performance issues because it is selecting a large number of records.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Apr 2006 22:44:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240279#M141884</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-11T22:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240280#M141885</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Or,  another way to go is to somehow get the  relevant order numbers, then hit that big table one at a time and get the versions for each order, then sort it, then read the first record of the itab.  Or you can do a select single and use the order by extension to get the last one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Apr 2006 22:54:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240280#M141885</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2006-04-11T22:54:47Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240281#M141886</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It will be difficult without knowing the structure of the table. Are these fields key fields or is at least one of them, a key field? How are the different versions inserted into the table? Is there a header-item kind of relationship or is it all in one table? Standard or custom?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Apr 2006 23:24:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240281#M141886</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-11T23:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240282#M141887</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The fields are key fields.   It is all one table.  For example, the ones in bold/italic are what I would want to select.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Order#  Version
12345      1
12345      2
&amp;lt;i&amp;gt;&amp;lt;b&amp;gt;12345      3&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;
67890      1
&amp;lt;i&amp;gt;&amp;lt;b&amp;gt;67890      2&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;
&amp;lt;i&amp;gt;&amp;lt;b&amp;gt;55555      1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;
33332      1
&amp;lt;i&amp;gt;&amp;lt;b&amp;gt;33332      2&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Apr 2006 00:57:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240282#M141887</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-12T00:57:08Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240283#M141888</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Denise,&lt;/P&gt;&lt;P&gt;You can use SQL aggregate functions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;TABLES: ztable.
DATA itab LIKE TABLE OF ztable WITH HEADER LINE.

SELECT ordernum max( version )
  FROM ztable
  INTO TABLE itab
  GROUP BY ordernum.

LOOP AT itab.
  WRITE: / itab-ordernum, itab-version.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ps: Reward points if helpful.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Wenceslaus.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Apr 2006 02:25:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240283#M141888</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-12T02:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240284#M141889</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This helps.  What if I want to select corresponding values from the table.  Can I do that.  For example, if I have this data:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;order#   vers   status
12345      1      S
12345      2      P
12345      3      A
88888      1      S
88888      2      P&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to return:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;order#   vers   status
12345      3      A
88888      2      P &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there any way to do that?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Apr 2006 13:36:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240284#M141889</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-12T13:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240285#M141890</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try this(assuming you always want the highest version which is also the latest)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SORT itab BY order# ASCENDING vers DESCENDING.&lt;/P&gt;&lt;P&gt;DELETE ADJACENT DUPLICATES FROM itab COMPARING order#.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Apr 2006 15:16:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240285#M141890</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-12T15:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: SQL SELECT</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240286#M141891</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Denise,&lt;/P&gt;&lt;P&gt;It wouldn't work if you have more than one column to select.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can add one more select query to fetch the rows for the corresponding maximum values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;TABLES: ztest.
DATA itab LIKE TABLE OF ztest WITH HEADER LINE.
DATA itab2 LIKE TABLE OF ztest WITH HEADER LINE.

SELECT ordernum max( version )
  FROM ztest
  INTO TABLE itab
  GROUP BY ordernum.

SELECT ordernum version status
  FROM ztest
  INTO TABLE itab2
  FOR ALL ENTRIES IN itab
  WHERE ordernum = itab-ordernum
    AND version  = itab-version.

LOOP AT itab2.
  WRITE: / itab2-ordernum, itab2-version, itab2-status.
ENDLOOP.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Wenceslaus.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Apr 2006 02:43:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/sql-select/m-p/1240286#M141891</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2006-04-13T02:43:31Z</dc:date>
    </item>
  </channel>
</rss>

