Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is pretty straight forward if you wanted to use a composite type, such as the native Point type, in your array. Something like this:</p> <pre><code>select dt.id, array_agg(point(p.x, p.y)) from pointtable p join ( select id, unnest(points) as p from linetable ) as dt on p.id = dt.p group by dt.id order by dt.id; </code></pre> <p>would give you this sort of output:</p> <pre><code> id | array_agg ----+------------------------------------------- 1 | {"(1,0)","(2,1)","(5,6)","(7,0)","(2,4)"} 2 | {"(2,4)","(4,4)","(7,0)","(5,6)","(2,1)"} 3 | {"(1,0)","(5,9)"} </code></pre> <p>Then you could work with the points.</p> <p>If you really want a two dimensional array then you can <a href="http://www.postgresql.org/docs/current/static/sql-createaggregate.html" rel="nofollow">create your own aggregate function</a>:</p> <pre><code>create aggregate array_cat_agg(int[]) (sfunc = array_cat, stype = int[], initcond = '{}'); </code></pre> <p>and then:</p> <pre><code>select dt.id, array_cat_agg(array[[p.x, p.y]]) from pointtable p join ( select id, unnest(points) as p from linetable ) as dt on p.id = dt.p group by dt.id order by dt.id; </code></pre> <p>to get this:</p> <pre><code> id | array_cat_agg ----+--------------------------------- 1 | {{1,0},{2,1},{5,6},{7,0},{2,4}} 2 | {{2,1},{5,6},{7,0},{2,4},{4,4}} 3 | {{1,0},{5,9}} </code></pre> <p>Note the <code>array[[...]]</code> trickery, you need that to make sure you get the right version of <code>array_cat</code>, if you just <code>array[...]</code> then you'll end up with a flattened array and that will make you sad. You can't just use <a href="http://www.postgresql.org/docs/current/static/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE" rel="nofollow"><code>array_agg</code></a> here as there's no such thing as an array-of-arrays and <code>array_agg(array[p.x, p.y])</code> wants to create an array-of-arrays. Hence the custom aggregate.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload