:
select
data[1] as data1,
data[2] as data2,
data[3] as data3
from
(select array_agg(data) as data from t) as t;
変数の列/行が(一つだけ多くの可能性から)カウントについて:その後、
create function prepare_statement(in p_name text, in p_body text) returns void as $$
declare
s text;
begin
s := 'prepare ' || p_name || ' as ' || p_body;
execute s;
return;
end; $$ language plpgsql;
と:
select prepare_statement('foo', (
select
'select ' ||
string_agg('data['||i||'] as data'||i, ', ') ||
' from (select array_agg(data) as data from t) as t'
from generate_series(1, (select count(*) from t)) n(i))
);
execute foo;
-- deallocate foo; -- to deallocate previously prepared statement
読みます
の詳細 arrays
array_agg function
prepare/execute/deallocate statements