<?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 DLT - deduplication pattern? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/dlt-deduplication-pattern/m-p/8194#M3902</link>
    <description>&lt;P&gt;Say we have an incremental append happening using autoloader, where filename is being added to the dataframe and that's all. If we want to de-duplicate this data in a rolling window, we can do something like &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;merge into logs 
using dedupedLogs 
   on logs.id = dedupedLogs.id and logs.date &amp;gt; current_date() - interval 7 days 
when matched and dedupedLogs.date &amp;gt; current_date() - interval 7 days then update set *
when not matched and dedupedLogs.date &amp;gt; current_date() - interval 7 days then insert *&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;where dedupedLogs is a subset of the incremental append (7 days) with an ordered hash key where rownum = 1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can this be done in delta live table? I've attempted using apply_changes() but keep getting an error that an upstream has changed. DLT's new algorithm doesn't seem to pick up that the second table, the de-duped one, should be an incremental table yet.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Mar 2023 16:56:48 GMT</pubDate>
    <dc:creator>Jfoxyyc</dc:creator>
    <dc:date>2023-03-06T16:56:48Z</dc:date>
    <item>
      <title>DLT - deduplication pattern?</title>
      <link>https://community.databricks.com/t5/data-engineering/dlt-deduplication-pattern/m-p/8194#M3902</link>
      <description>&lt;P&gt;Say we have an incremental append happening using autoloader, where filename is being added to the dataframe and that's all. If we want to de-duplicate this data in a rolling window, we can do something like &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;merge into logs 
using dedupedLogs 
   on logs.id = dedupedLogs.id and logs.date &amp;gt; current_date() - interval 7 days 
when matched and dedupedLogs.date &amp;gt; current_date() - interval 7 days then update set *
when not matched and dedupedLogs.date &amp;gt; current_date() - interval 7 days then insert *&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;where dedupedLogs is a subset of the incremental append (7 days) with an ordered hash key where rownum = 1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can this be done in delta live table? I've attempted using apply_changes() but keep getting an error that an upstream has changed. DLT's new algorithm doesn't seem to pick up that the second table, the de-duped one, should be an incremental table yet.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Mar 2023 16:56:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dlt-deduplication-pattern/m-p/8194#M3902</guid>
      <dc:creator>Jfoxyyc</dc:creator>
      <dc:date>2023-03-06T16:56:48Z</dc:date>
    </item>
    <item>
      <title>Re: DLT - deduplication pattern?</title>
      <link>https://community.databricks.com/t5/data-engineering/dlt-deduplication-pattern/m-p/8195#M3903</link>
      <description>&lt;P&gt;@Jordan Fox​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you're getting an error about upstream changes, it might be because the table schema or partitioning has changed. You can try running DESCRIBE EXTENDED logs and DESCRIBE EXTENDED dedupedLogs to compare the schemas and see if there are any differences.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes, it is possible to perform a rolling window de-duplication in Delta Lake using the merge operation. You can merge the incremental data with the existing data in the Delta table and update or insert records based on a condition. Giving an example as below&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;MERGE INTO logs
USING (
  SELECT id, filename, date 
  FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY date DESC) as rownum
    FROM dedupedLogs
    WHERE date &amp;gt; current_date() - interval 7 days
  ) WHERE rownum = 1
) AS d
ON logs.id = d.id AND logs.date &amp;gt; current_date() - interval 7 days
WHEN MATCHED THEN UPDATE SET logs.filename = d.filename
WHEN NOT MATCHED THEN INSERT (id, filename, date) VALUES (d.id, d.filename, d.date);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this example, dedupedLogs is the table that contains the de-duplicated data for the past 7 days. We use the ROW_NUMBER() window function to assign a row number to each record within a group of records with the same  id, ordered by the date column in descending order. We then select only the records with rownum = 1 to get the most recent record for each id. The MERGE INTO statement matches records in logs with records in d using the  id column, and filters them by the date column. If a match is found, the filename column in logs is updated with the value from d. If there is no match, a new record is inserted into logs with the values from d.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2023 00:47:02 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dlt-deduplication-pattern/m-p/8195#M3903</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-03-13T00:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: DLT - deduplication pattern?</title>
      <link>https://community.databricks.com/t5/data-engineering/dlt-deduplication-pattern/m-p/8196#M3904</link>
      <description>&lt;P&gt;Hi @Jordan Fox​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We'd love to hear from you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2023 00:10:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dlt-deduplication-pattern/m-p/8196#M3904</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-04-01T00:10:31Z</dc:date>
    </item>
  </channel>
</rss>

