<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title><![CDATA[Jake McCrary's articles on sql]]></title>
  <link href="https://jakemccrary.com/atom.xml" rel="self"/>
  <link href="https://jakemccrary.com/"/>
  <updated>2026-08-17T20:14:30+00:00</updated>
  <id>https://jakemccrary.com/</id>
  <author>
    <name><![CDATA[Jake McCrary]]></name>
  </author>
  <entry>
    <id>https://jakemccrary.com/blog/2015/11/15/sql-aggregate-a-set-of-values-together/index.html</id>
    <link href="https://jakemccrary.com/blog/2015/11/15/sql-aggregate-a-set-of-values-together/index.html"/>
    <title><![CDATA[SQL: Aggregate a set of values together]]></title>
    <updated>2015-11-15T23:59:59+00:00</updated>
    <content type="html"><![CDATA[<div><p>Lately I&apos;ve been working on projects that use <a href="http://www.postgresql.org/">Postgres</a> as our relational database. This has allowed us to simplify some of our Clojure code by leaning on some built-in features of Postgres. One SQL function supported by Postgres which has greatly simplified our code is the <code>array_agg</code> <a href="http://www.postgresql.org/docs/9.4/static/functions-aggregate.html">aggregate</a> function.</p><h2 id="what-is-array-agg?">What is <code>array_agg</code>?</h2><p>The <code>array_agg</code> function takes an argument and returns an array of the argument type. That sentence will make more sense after an example. The snippet below shows a simplified schema for a blog&apos;s database. There is a table called <code>blog_posts</code> that contains details about posts, a table called <code>categories</code> that has labels that can be applied to blog posts, and a join table called <code>post_categories</code> that links the two previous tables together.</p><pre><code class="language-sql">blog=# select id, title from blog_posts;
 id |    title
----+--------------
  1 | SQL Post
  2 | Clojure Post

blog=# select * from categories;
 id |   name
----+----------
  1 | sql
  2 | emacs
  3 | clojure
  4 | postgres

blog=# select * from post_categories;
 blog_post_id | category_id
--------------+-------------
            1 |           1
            2 |           2
            1 |           4
            2 |           3
</code></pre><p>Before I learned about <code>array_agg</code>, if I wanted to know how each blog post had been categorized I might have written the following query.</p><pre><code>select title, name as category
  from blog_posts bp
  join post_categories pc on pc.blog_post_id = bp.id
  join categories c on c.id = pc.category_id
  order by title;


    title     | category
--------------+----------
 Clojure Post | emacs
 Clojure Post | clojure
 SQL Post     | sql
 SQL Post     | postgres
</code></pre><p>The result is readable but as the number of posts and categories grow it becomes harder to read. The query also doesn&apos;t answer the question, &quot;How are my posts categorized?&quot;, well. The ideal answer is a single row per post that shows the post&apos;s categories. You can use <code>array_agg</code> to get that ideal answer.</p><pre><code class="language-sql">select title, array_agg(name) as categories
  from blog_posts bp
  join post_categories pc on pc.blog_post_id = bp.id
  join categories c on c.id = pc.category_id
  group by title;

    title     |   categories
--------------+-----------------
 SQL Post     | {sql,postgres}
 Clojure Post | {emacs,clojure}
</code></pre><p>I find the <code>array_agg</code> version much nicer to read. The result answers the question in a very direct fashion and the query expresses the question well. Everything about the query expresses the question, you no longer have an extra <code>order by</code> clause to make the result more readable by human eyes.</p><h2 id="how-did-it-make-my-clojure-code-simpler?">How did it make my Clojure code simpler?</h2><p>The above is great and it makes everything more readable for a human. Most of the time I&apos;m not querying a SQL database so that a human can directly read the results; instead I&apos;m using Clojure to manipulate results of a query. Fortunately, <code>array_agg</code> simplifies my Clojure code as well.</p><p>I&apos;m working with a schema that has many relationships similar to the above relationship. Continuing with the example from above the snippet below shows the data shape we&apos;d get back from <code>clojure.java.jdbc</code> prior to using <code>array_agg</code>. The data shape we actually want follows.</p><pre><code class="language-clojure">;; data shape you get from the non-array_agg query.
[{:title &quot;Clojure Post&quot; :category &quot;emacs&quot;}
 {:title &quot;SQL Post&quot; :category &quot;sql&quot;}
 {:title &quot;Clojure Post&quot; :category &quot;clojure&quot;}
 {:title &quot;SQL Post&quot; :category &quot;postgres&quot;}]

;; data shape you want
[{:title &quot;Clojure Post&quot; :categories [&quot;emacs&quot; &quot;clojure&quot;]}
 {:title &quot;SQL Post&quot; :categories [&quot;sql&quot; &quot;postgres&quot;]}]
</code></pre><p>Since we&apos;re not getting data in our desired shape we need to write code that combines rows. One way of doing that is to use <code>reduce</code> and <code>map</code>.</p><pre><code class="language-clojure">(defn squash-by-title [rows]
  (-&gt;&gt; rows
       (reduce (fn [r row] (update r (:title row) conj (:category row))) {})
       (map (fn [[title categories]] {:title title :categories categories}))))
</code></pre><p>I&apos;ve been writing Clojure for a long time and when I see code like above it still takes me a bit of time to figure out what is happening. Not only that, but eventually your project has different squash operations depending on what data you&apos;re pulling back from the database. They are probably mostly similar and eventually you abstract the differences and feel great. Then you come back months later and have to figure out how it all works. Luckily, if you&apos;re using a database that supports <code>array_agg</code>, there is a better way.</p><p>The first step is to change your queries to use <code>array_agg</code>. The second step is to extend the <code>clojure.java.jdbc/IResultSetReadColumn</code> protocol to the type returned by your jdbc driver. For my project that looks like the following code:</p><pre><code class="language-clojure">;; clojure.java.jdbc has been required as jdbc

(extend-protocol jdbc/IResultSetReadColumn
  org.postgresql.jdbc4.Jdbc4Array
  (result-set-read-column [pgobj metadata i]
    (vec (.getArray pgobj))))
</code></pre><p>By changing my queries to use <code>array_agg</code> and adding those four lines of code I&apos;m able to delete all of my squashing functions and get data from my database in the shape I want. I also end up with easier to understand code and more expressive queries. Awesome.</p><p><em>Thanks to <a href="http://timothypratley.blogspot.com/">Timothy Pratley</a> for providing feedback on earlier versions of this post.</em></p></div>]]></content>
  </entry>
</feed>
