我有以下数据:
letter number
A 1
A 2
B 3
B 4
C 5
C 6
如何在Postgres中编写一个select语句,将这些数据选择到一个包含每个字母所有可能数字数组的表中?
letter number array
A {1,2}
B {3,4}
C {5,6}
答案 0 :(得分:3)
select letter, array_agg(number) as numbers
from the_table
group by letter;
另见手册:http://www.postgresql.org/docs/current/static/functions-aggregate.html
答案 1 :(得分:0)
如果您的Postgres版本没有“array_agg”功能,您可以尝试这样:
select letter,
array(
select number
from the_table t2
where t2.letter = t1.letter
order by number
) as numbers
from the_table t1
group by letter;