1
저는 11 개의 숫자 요소로 구성된 json 배열을 포함하는 속성 테이블을 가지고 있습니다. json 배열의 데이터에 대해 일부 집계 (공상, 평균, 최대/최소 통계 등)를 수행해야합니다.PostgreSQL에서 json 열의 데이터 압축을 풉니 다
일반화 된 답변 here은 빠른 쿼리를 위해 데이터를 임시보기로 다시 포맷하는 데 적합합니다. jsonb_each를 son_each로 바꿨는데 (어쩌면 이렇게 될까요?) 오류가 없지만 데이터가없는 'create_jsonb_pivot_view'라는 단일 열로 끝납니다. 그래서 이것은 나를 위해 새로운 영토
select create_jsonb_pivot_view('table', 'A, B, C, D', 'params')
내가 전에 SQL에서 함수를 사용한 적이 : 함수를 사용
create or replace function create_jsonb_pivot_view
(table_name text, regular_columns text, json_column text)
returns void language plpgsql as $$
declare
s text;
begin
execute format ($fmt$
select string_agg(format('%s->>''%s'' "%s"', key, key), ',')
from (
select distinct key
from %s, json_each(%s)
order by 1
) s;
$fmt$, json_column, '%s', '%s', table_name, json_column)
into s;
execute format('
drop view if exists %s_view;
create view %s_view as
select %s, %s from %s',
table_name, table_name, regular_columns, s, table_name);
end $$;
그리고 마지막으로 쿼리 : 여기
는 기능입니다. 아마도 내 목표를 함께 달성하기위한 다른 방법이 있을까요?