우선 해야했던 작업은 sql SELECT문에서 10의 9개의 컬럼은 일반적으로 값을 하나씩 넣어서 세팅해주면 되었던 column들이였고 10의 1개가 하나의 컬럼안에 여러 값을 배열([])형태로 담아서 보내야하는 사항이였다.
하나의 Column안에 배열 형태로 값을 넣는방법은 다음과 같다.
array_to_json(array_agg(crq.*))
해당 부분은 select의 subQuery로 넣으면 된다.
select
id,
status,
...,
(
SELECT
array_to_json(array_agg(tn.*))
FROM
tableName tn
)
from table
와 같이 사용하면 된다.
여기서 하나의 문제
위 코드에
order by tn.created_at desc
를 심으면 AGGREGATE ERROR가 난다. 집계함수를 썻기에 ORDER BY를 쓸 수 없단 말이다.
따라서 이럴때는
select
id,
status,
...,
(
SELECT
array_to_json(array_agg(tn.*))
FROM
tableName tn
)
from table
order by tn.created_at desc
위 Error 나는 코드를
select
id,
status,
...,
(
SELECT
array_to_json(array_agg(tn.* order by tn.created_at desc))
FROM
tableName tn
)
from table
와 같이 수정 하면 끝!