Home > OS >  Get first item of 2d array entries in postgreSQL
Get first item of 2d array entries in postgreSQL

Time:03-22

I got a postgres function that gives me this (sample) 2D array

{{0.82,60.64,1006},{2.0,59.64,1006},{9.9,60.304,999}}

I want to get the first item of each row, like this:

{0.82,2.0,9.9,}

Any way of doing this directly on Postgres/SQL? Thanks

CodePudding user response:

PostrgeSql provides array ops you want. Demo

CREATE TABLE sample (
    id int,
    va decimal[3][3]
);
INSERT INTO sample
    VALUES (1, '{{0.82,60.64,1006},{2.0,59.64,1006},{9.9,60.304,999}}');

select id, array_agg(v) res
from (
  select id, unnest(va[:][1:1]) v
  from sample
) t
group by id

Returns

id  res
1   {0.82,2.0,9.9}
  • Related