<?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>Question Re: SqlScript delete duplicates from local table variable in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615337#M148960</link>
    <description>&lt;P&gt;Now this is a prime example for why one should try to ask about how to achieve something instead of how to do this little step in the overall solution.&lt;/P&gt;
  &lt;P&gt;Your problem actually is: get me the most current months record for every combination of material, factory and year.&lt;/P&gt;
  &lt;P&gt;There's a fairly straight forward approach for that.&lt;/P&gt;
  &lt;OL&gt;
   &lt;LI&gt;find out the most current month for every material|factory|year.&lt;/LI&gt;
   &lt;LI&gt;select only those records from the table that are about the most current month. &lt;/LI&gt;
  &lt;/OL&gt;
  &lt;P&gt;Step 1 is an easy MAX() aggregation.&lt;/P&gt;
  &lt;P&gt;Step 2 is an easy filtering via INNER JOIN&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;select s.matnr, s.werks, s.gjahr, s.monat, s.stock
from stock s
inner join (select matnr, werks, gjahr, max(monat) as max_month
            from stock
            group by matnr, werks, gjahr) ms
       on ( s.matnr, s.werks, s.gjahr, s.monat) 
          = (ms.matnr, ms.werks, ms.gjahr, ms.max_month);&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;With your demo data this yields:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;MATNR       WERKS   GJAHR   MONAT   STOCK
1000000000  1000    2017    05      0    
1000000001  1000    2018    03      3    &lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 15 Mar 2018 06:10:33 GMT</pubDate>
    <dc:creator>lbreddemann</dc:creator>
    <dc:date>2018-03-15T06:10:33Z</dc:date>
    <item>
      <title>SqlScript delete duplicates from local table variable</title>
      <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaq-p/615334</link>
      <description>&lt;P&gt;Hi, experts.&lt;/P&gt;
  &lt;P&gt;I have a issue. I need to delete duplicates from local table variable in AMDP.&lt;/P&gt;
  &lt;P&gt;I find this &lt;/P&gt;
  &lt;P&gt;delete from tab&lt;BR /&gt;where "$rowid$" in&lt;BR /&gt;(&lt;BR /&gt; SELECT LEAD($rowid$) over (partition by field1, field2) from tab&lt;BR /&gt;) ;&lt;/P&gt;
  &lt;P&gt;But for local table variable it is do not work .&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 03:51:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaq-p/615334</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-03-15T03:51:55Z</dc:date>
    </item>
    <item>
      <title>Re: SqlScript delete duplicates from local table variable</title>
      <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615335#M148958</link>
      <description>&lt;P&gt;DELETE on table variables is only available starting with HANA 2. Do you use a HANA 2?&lt;/P&gt;
  &lt;P&gt;Besides that, using the internal column store column $rowid$ has never been supported - using it to eliminate duplicate records in the absence of a key is not the brightest idea.&lt;/P&gt;
  &lt;P&gt;What you can easily do in your case is to simply SELECT DISTINCT from your table variable.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;tab_dupes = SELECT ... FROM all_my_dupes;
tab_nodupes = SELECT DISTINCT * FROM :tab_dupes;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;That's it. No weird internal column stuff, no difficult update on table variables. &lt;/P&gt;
  &lt;P&gt;For scenarios where you have duplicates only on the subset of columns, e.g. your key-columns to-be, you can use the standard options like picking the largest value (if "largest" is unique) or just get the first/last entry. But that's all standard SQL, too...&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 04:36:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615335#M148958</guid>
      <dc:creator>lbreddemann</dc:creator>
      <dc:date>2018-03-15T04:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: SqlScript delete duplicates from local table variable</title>
      <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615336#M148959</link>
      <description>&lt;P&gt;Thanks, Lars.&lt;/P&gt;
  &lt;P&gt;I am using Hana 1.0 SPS12.&lt;/P&gt;
  &lt;P&gt;I am trying to using DISTINCT but more complex task.&lt;/P&gt;
  &lt;P&gt;I have i db table with stocks history.For example&lt;/P&gt;
  &lt;P&gt;matnr werks gjahr monat stock&lt;/P&gt;
  &lt;P&gt;1000000000 1000 2017 05 0&lt;/P&gt;
  &lt;P&gt;1000000000 1000 2017 04 2&lt;/P&gt;
  &lt;P&gt;1000000000 1000 2017 01 1&lt;/P&gt;
  &lt;P&gt;1000000001 1000 2018 02 2 &lt;/P&gt;
  &lt;P&gt;1000000001 1000 2018 03 3&lt;/P&gt;
  &lt;P&gt;1000000001 1000 2018 01 1&lt;/P&gt;
  &lt;P&gt;And i need to get the last stock for each group of (matnr+werks)&lt;/P&gt;
  &lt;P&gt;For using distinct i need to select only matnr+werks, but how i can get last stock number ?&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 04:59:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615336#M148959</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-03-15T04:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: SqlScript delete duplicates from local table variable</title>
      <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615337#M148960</link>
      <description>&lt;P&gt;Now this is a prime example for why one should try to ask about how to achieve something instead of how to do this little step in the overall solution.&lt;/P&gt;
  &lt;P&gt;Your problem actually is: get me the most current months record for every combination of material, factory and year.&lt;/P&gt;
  &lt;P&gt;There's a fairly straight forward approach for that.&lt;/P&gt;
  &lt;OL&gt;
   &lt;LI&gt;find out the most current month for every material|factory|year.&lt;/LI&gt;
   &lt;LI&gt;select only those records from the table that are about the most current month. &lt;/LI&gt;
  &lt;/OL&gt;
  &lt;P&gt;Step 1 is an easy MAX() aggregation.&lt;/P&gt;
  &lt;P&gt;Step 2 is an easy filtering via INNER JOIN&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;select s.matnr, s.werks, s.gjahr, s.monat, s.stock
from stock s
inner join (select matnr, werks, gjahr, max(monat) as max_month
            from stock
            group by matnr, werks, gjahr) ms
       on ( s.matnr, s.werks, s.gjahr, s.monat) 
          = (ms.matnr, ms.werks, ms.gjahr, ms.max_month);&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;With your demo data this yields:&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;MATNR       WERKS   GJAHR   MONAT   STOCK
1000000000  1000    2017    05      0    
1000000001  1000    2018    03      3    &lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Mar 2018 06:10:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615337#M148960</guid>
      <dc:creator>lbreddemann</dc:creator>
      <dc:date>2018-03-15T06:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: SqlScript delete duplicates from local table variable</title>
      <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615338#M148961</link>
      <description>&lt;P&gt;Thanks, Lars.&lt;/P&gt;
  &lt;P&gt;It is resolve my problem.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Mar 2018 06:55:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615338#M148961</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-03-15T06:55:12Z</dc:date>
    </item>
    <item>
      <title>Re: SqlScript delete duplicates from local table variable</title>
      <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615339#M148962</link>
      <description>&lt;P&gt;To identify and &lt;A href="http://www.kodyaz.com/articles/delete-duplicate-rows-using-row-number-partition-by-order-by.aspx" target="_blank"&gt;delete duplicates using SQLScript&lt;/A&gt;, another method can be using Row_Number() with Partition By clause. The columns that are accepted to define duplicate case can be used in Partition By clause of the Row_Number() function.&lt;/P&gt;
  &lt;P&gt;Here is a sample&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;et_data = select kunnr, vbeln from (
    select row_number ( ) over ( partition by kunnr order by vbeln) as rn, * from :lt_data
    ) where rn = 1;
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Above SQL returns first order of a customer&lt;/P&gt;
  &lt;P&gt;If you add more columns in Partition By clause (here is only kunnr used), or all columns than you will have an exact duplicate of the row data.&lt;/P&gt;
  &lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 10:45:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615339#M148962</guid>
      <dc:creator>eralper_yilmaz</dc:creator>
      <dc:date>2018-04-20T10:45:17Z</dc:date>
    </item>
    <item>
      <title>Re: SqlScript delete duplicates from local table variable</title>
      <link>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615340#M148963</link>
      <description>&lt;P&gt;Thanks, &lt;A href="https://answers.sap.com/users/51646/eralperyilmaz.html"&gt;ERALPER YILMAZ&lt;/A&gt; &lt;/P&gt;
  &lt;P&gt;It is a best way to delete duplicates.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Apr 2018 04:03:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sqlscript-delete-duplicates-from-local-table-variable/qaa-p/615340#M148963</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-04-23T04:03:33Z</dc:date>
    </item>
  </channel>
</rss>

