Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:21:35+00:00 2026-06-08T20:21:35+00:00

I have the following data: name id url John 1 someurl.com Matt 2 cool.com

  • 0

I have the following data:

name          id             url

John          1              someurl.com
Matt          2              cool.com
Sam           3              stackoverflow.com

How can I write an SQL statement in Postgres to select this data into a multi-dimensional array, i.e.:

{{John, 1, someurl.com}, {Matt, 2, cool.com}, {Sam, 3, stackoverflow.com}}

I’ve seen this kind of array usage before in Postgres but have no idea how to select data from a table into this array format.

Assuming here that all the columns are of type text.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:21:37+00:00Added an answer on June 8, 2026 at 8:21 pm

    You cannot use array_agg() to produce multi-dimensional arrays, at least not up to PostgreSQL 9.4.
    (But the upcoming Postgres 9.5 ships a new variant of array_agg() that can!)

    What you get out of @Matt Ball’s query is an array of records (the_table[]).

    An array can only hold elements of the same base type. You obviously have number and string types. Convert all columns (that aren’t already) to text to make it work.

    You can create an aggregate function for this like I demonstrated to you here before.

    CREATE AGGREGATE array_agg_mult (anyarray)  (
        SFUNC     = array_cat
       ,STYPE     = anyarray
       ,INITCOND  = '{}'
    );
    

    Call:

    SELECT array_agg_mult(ARRAY[ARRAY[name, id::text, url]]) AS tbl_mult_arr
    FROM   tbl;
    

    Note the additional ARRAY[] layer to make it a multidimensional array (2-dimenstional, to be precise).

    Instant demo:

    WITH tbl(id, txt) AS (
        VALUES
          (1::int, 'foo'::text)
         ,(2,      'bar')
         ,(3,      '}b",') -- txt has meta-characters
        )
        , x AS (
        SELECT array_agg_mult(ARRAY[ARRAY[id::text,txt]]) AS t
        FROM   tbl
        )
    SELECT *, t[1][3] AS arr_element_1_1, t[3][4] AS arr_element_3_2
    FROM   x;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml like the following <DataCollection> <Data> <Name>John</Name> <Age>30</Age> </Data> ... more
I have got following folder structure for my domain (domain.com): /data/images (it´s subdomain name,
I have the following script in my javascript... $.ajax({ type: 'POST', url: 'http://www.example.com/ajax', data:
I have the following sample data: Id Name Quantity 1 Red 1 2 Red
I have a table emp with following structure and data: name dept salary -----
Lets say i have a table with the following data Customer table: Name amount
I have to parse the following data {ResultSet:{Query:microsec fin,Result: [{symbol:MICROSE_a.NS,name: MICROSEC FIN SERV LTD
I have following code Ext.regModel('Centre', { fields: ['name', 'url'] }); Ext.application({ name: 'Sencha', launch:
I have the following code: jQuery: $.ajax({ url: '/personcontroller/getperson', cache: false, type: POST, data:
I have the following: $('#EID').change(function () { $.ajax({ url: /Administration/stats, data: { DataSource: $('#DataSource').val(),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.