<?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: How to explode an array column and repack the distinct values into one array in DB SQL? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14853#M9278</link>
    <description>&lt;P&gt;try to use SQL windows functions here&lt;/P&gt;</description>
    <pubDate>Sat, 24 Dec 2022 04:35:31 GMT</pubDate>
    <dc:creator>Aviral-Bhardwaj</dc:creator>
    <dc:date>2022-12-24T04:35:31Z</dc:date>
    <item>
      <title>How to explode an array column and repack the distinct values into one array in DB SQL?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14850#M9275</link>
      <description>&lt;P&gt;Hi, I am new to DB SQL. I have a table where the array column (cities) contains multiple arrays and some have multiple duplicate values. I need to unpack the array values into rows so I can list the distinct values. The following query works for this step.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;SELECT distinct 
key, 
explode(cities) as cities
FROM green.cities
group by key, cities&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The next step I want to repack the distinct cities into one array grouped by key.&lt;/P&gt;&lt;P&gt;I can do this easily in pyspark using two dataframes, first by doing an explode on the array column of the first dataframe and then doing a collect_set on the same column in the next dataframe. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any suggestions how I can do this just using Databricks SQL?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2022 14:07:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14850#M9275</guid>
      <dc:creator>test_user</dc:creator>
      <dc:date>2022-12-23T14:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to explode an array column and repack the distinct values into one array in DB SQL?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14851#M9276</link>
      <description>&lt;P&gt;You can use collect_set in SQL. If you share the example input row and desired output, it would be easier to help.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2022 14:29:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14851#M9276</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2022-12-23T14:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to explode an array column and repack the distinct values into one array in DB SQL?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14852#M9277</link>
      <description>&lt;P&gt;Thanks Hubert. What i tried so far is not collecting the distinct values of the expolded column grouped by key.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;An example input is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;key, cities&lt;/B&gt;&lt;/P&gt;&lt;P&gt;1, ["milan","paris","new york"]&lt;/P&gt;&lt;P&gt;1, ["London"]&lt;/P&gt;&lt;P&gt;1, ["London","paris"]&lt;/P&gt;&lt;P&gt;1, ["London","paris"]&lt;/P&gt;&lt;P&gt;1, ["London","paris"]&lt;/P&gt;&lt;P&gt;1, ["milan","paris"]&lt;/P&gt;&lt;P&gt;1, ["paris","new york"]&lt;/P&gt;&lt;P&gt;1, ["new york"]&lt;/P&gt;&lt;P&gt;1, ["new york"]&lt;/P&gt;&lt;P&gt;2, ["milan","paris","new york"]&lt;/P&gt;&lt;P&gt;2, ["paris"]&lt;/P&gt;&lt;P&gt;2, ["paris"]&lt;/P&gt;&lt;P&gt;2, ["milan","paris"]&lt;/P&gt;&lt;P&gt;2, ["paris","new york"]&lt;/P&gt;&lt;P&gt;2, ["Tokyo"]&lt;/P&gt;&lt;P&gt;2, ["new york"]&lt;/P&gt;&lt;P&gt;2, ["LA","Tokyo"]&lt;/P&gt;&lt;P&gt;2, ["LA","Tokyo"]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The desired output is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;key, cities&lt;/B&gt;&lt;/P&gt;&lt;P&gt;1, ["milan","paris","new york","London"]&lt;/P&gt;&lt;P&gt;2, ["milan","paris","new york","LA","Tokyo"]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2022 14:45:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14852#M9277</guid>
      <dc:creator>test_user</dc:creator>
      <dc:date>2022-12-23T14:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to explode an array column and repack the distinct values into one array in DB SQL?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14853#M9278</link>
      <description>&lt;P&gt;try to use SQL windows functions here&lt;/P&gt;</description>
      <pubDate>Sat, 24 Dec 2022 04:35:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-explode-an-array-column-and-repack-the-distinct-values/m-p/14853#M9278</guid>
      <dc:creator>Aviral-Bhardwaj</dc:creator>
      <dc:date>2022-12-24T04:35:31Z</dc:date>
    </item>
  </channel>
</rss>

