简体   繁体   English

在复合类型数组中,如何为数组中的所有条目选择复合类型的第一个元素?

[英]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: 现在,我想要的是一个“ select”语句,该语句为数组中的每个条目显示元素“ a”,预期可能是这样的:

|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 . 您应该考虑在表中添加一个id列,以便在取消unnest之后可以对该id进行分组。

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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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