<?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: Agg items in a map in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/agg-items-in-a-map/m-p/123078#M46939</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/96460"&gt;@AdamIH123&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;The explode-based approach is widely used and remains the most reliable and readable method.&lt;/P&gt;&lt;P&gt;But if you're looking for an alternative without using explode, you can try the REDUCE + MAP_FILTER approach. It lets you aggregate maps across rows efficiently:&lt;/P&gt;&lt;P&gt;SELECT&lt;BR /&gt;&amp;nbsp; id,&lt;BR /&gt;&amp;nbsp; MAP_FILTER(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; REDUCE(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; COLLECT_LIST(color_map),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; MAP('init', 0),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; (acc, m) -&amp;gt; MAP_ZIP_WITH(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; acc,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (k, v1, v2) -&amp;gt; COALESCE(v1, 0) + COALESCE(v2, 0)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;BR /&gt;&amp;nbsp; &amp;nbsp; ),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; (k, v) -&amp;gt; k != 'init'&lt;BR /&gt;&amp;nbsp; ) AS aggregated_color_map&lt;BR /&gt;FROM cte&lt;BR /&gt;GROUP BY id;&lt;/P&gt;</description>
    <pubDate>Fri, 27 Jun 2025 15:09:25 GMT</pubDate>
    <dc:creator>SP_6721</dc:creator>
    <dc:date>2025-06-27T15:09:25Z</dc:date>
    <item>
      <title>Agg items in a map</title>
      <link>https://community.databricks.com/t5/data-engineering/agg-items-in-a-map/m-p/122889#M46897</link>
      <description>&lt;P&gt;What is the best way to aggregate a map across rows? In the below, The agg results would be red: 4, green 7, blue: 10. This can be achieved using explode wondering if there is a better way.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%sql 

with cte as (
select 
1 as id 
, map('red', 1, 'green' 2) as color_map
union all 
select 
1 as id 
, map('red', 3, 'green' 5, 'blue': 10) as color_map
)

select 
id
&amp;lt;agg color map so final output is red 4 green 7 blue 10&amp;gt;
from cte 

group by id &lt;/LI-CODE&gt;&lt;P&gt;The below works but uses an explode.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%sql
with cte as (
select 
1 as id 
, map('red', 1, 'green', 2) as color_map
union all 
select 
1 as id 
, map('red', 3, 'green', 5, 'blue', 10) as color_map
),
exploded as (
  select 
    id,
    explode(color_map) as (key, value)
  from cte
),
aggregated as (
  select 
    id,
    key,
    sum(value) as sum_value
  from exploded
  group by id, key
)
select 
  id,
  map_from_arrays(
    collect_list(key),
    collect_list(sum_value)
  ) as aggregated_color_map
from aggregated
group by id&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jun 2025 23:15:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/agg-items-in-a-map/m-p/122889#M46897</guid>
      <dc:creator>AdamIH123</dc:creator>
      <dc:date>2025-06-25T23:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Agg items in a map</title>
      <link>https://community.databricks.com/t5/data-engineering/agg-items-in-a-map/m-p/123078#M46939</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/96460"&gt;@AdamIH123&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;The explode-based approach is widely used and remains the most reliable and readable method.&lt;/P&gt;&lt;P&gt;But if you're looking for an alternative without using explode, you can try the REDUCE + MAP_FILTER approach. It lets you aggregate maps across rows efficiently:&lt;/P&gt;&lt;P&gt;SELECT&lt;BR /&gt;&amp;nbsp; id,&lt;BR /&gt;&amp;nbsp; MAP_FILTER(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; REDUCE(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; COLLECT_LIST(color_map),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; MAP('init', 0),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; (acc, m) -&amp;gt; MAP_ZIP_WITH(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; acc,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (k, v1, v2) -&amp;gt; COALESCE(v1, 0) + COALESCE(v2, 0)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; )&lt;BR /&gt;&amp;nbsp; &amp;nbsp; ),&lt;BR /&gt;&amp;nbsp; &amp;nbsp; (k, v) -&amp;gt; k != 'init'&lt;BR /&gt;&amp;nbsp; ) AS aggregated_color_map&lt;BR /&gt;FROM cte&lt;BR /&gt;GROUP BY id;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jun 2025 15:09:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/agg-items-in-a-map/m-p/123078#M46939</guid>
      <dc:creator>SP_6721</dc:creator>
      <dc:date>2025-06-27T15:09:25Z</dc:date>
    </item>
  </channel>
</rss>

