PostgreSQL 저장 함수로 작업 중입니다. JSON 입력에서 값을 추출하고 테이블의 열과 일치시키고 테이블을 JSON 형식으로 반환해야합니다. 내 JSON 입력,
{
"sensor":"sensor1",
"fromdate":date,
"todate":"date
}
여기 내 센서 테이블 은 지금까지 작업 한 기능입니다.
select array_to_json(array_agg(row_to_json(d)))
from (
select sensor,id,value,created_date
from probe_data
where probe_data.sensor =sensors
AND probe_data.created_date >=fromdate AND probe_data.created_date <= todate
) d
select x.sensores,x.fromdate,x.todate from json_to_recordset($1) x
(
sensors text,
fromdate timestamp,
todate timestamp)
보시다시피 JSON 입력에서 데이터를 가져올 수 있지만 WHERE 조건 내에서 데이터에 액세스하는 방법을 모르겠습니다. 이렇게하려면 도움이 필요합니다.
간단히 JSONB
연산자를 사용 하여 아래와 같이이를 수행 할 수 있습니다 .
이 시도:
create or replace function example (param1 jsonb)
returns table (sensor_ varchar, id_ int, value_ numeric, created_date_ TIMESTAMP)
as
$body$
begin
RETURN query
select sensor, id, value, created_date from probe_data
where
created_date >=(param1->>'fromdate')::TIMESTAMP
and
created_date <= (param1->>'todate')::TIMESTAMP;
end;
$body$
LANGUAGE plpgsql;
위의 출력을 JSON ARRAY
형식으로 원하면
이 시도:
create or replace function example1 (param1 jsonb)
returns jsonb
as
$body$
declare
jsondata jsonb;
begin
jsondata = (select
array_to_json(array_agg(row_to_json(d))) from (
select sensor, id, value, created_date from probe_data
where
created_date >=(param1->>'fromdate')::TIMESTAMP
and
created_date <= (param1->>'todate')::TIMESTAMP ) d);
RETURN jsondata;
end;
$body$
LANGUAGE plpgsql;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다