简体   繁体   中英

In an composite type array, how should I select first element of composite type for all entries in array?

I have a composite type:

create type p as (a int, b int);

Then I created an table:

create table f(pv p[]);

Fill in value with it:

insert into f values(array[(10,20),(30,40)]::p[]);
insert into f values(array[(1,20)]::p[]);

Now what I want is a "select" statement that display element "a" for each entry in the array, the expected may be like:

|pv     |
|{10,30}|
|{1}    |

I've tired lots of statement combination but it doesn't provide the answer.

Can anyone help?

Thank you!

Shore

You should consider adding an id column to your table, so that the grouping can be done on that id after unnest ing .

SELECT array_agg(a)
FROM f
    ,unnest(pv)
GROUP BY id;

Otherwise, you could group by the whole array

SELECT array_agg(a)
FROM f
    ,unnest(pv)
GROUP BY pv;

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM