복잡한 SQL 쿼리를 한 문장에 담아서 작성하다보면 Nested Select 쿼리를 쓰게 되고 그 결과 값들이 다시 Join 하는 케이스가 많이 발생합니다. 그런데 문제는 Nested Select 쿼리의 cost가 높아서 얼마 안되는 레코드 수에도 쿼리 실행 속도가 말도안되게 느려지는 문제가 발생합니다. 그래서 이런 nested select 쿼리를 피하기 위해 temporary table을 생성 후 중간 쿼리 결과를 이곳에 담아 놓고 이 임시 테이블들 간에 join을 해서 최종 결과를 뽑으면 훨씬 빠른 속도로 원하는 데이터를 추출 할 수 있습니다. PostgreSQL에서는 아래와 같은 방법으로 Temporary 테이블을 생성 할 수 있습니다. create temp table temp_test ( "Id"..
json_array_elements() 함수를 사용하면 json 타입의 컬럼에서 json 내의 배열 property의 값을 행으로 가져올 수 있습니다. with test as (select '{"array": [ "v1", "v2", "v3", "v4", "v5" ]}'::json json) select j.* from test, json_array_elements(json -> 'array') j
1. 구분자로 문자열을 분리한 다음 각각의 컬럼으로 나누기 with test as (select '서울,대구,부산,대전,인천,광주' cities) select split_part(cities, ',', 1) "1", split_part(cities, ',', 2) "2", split_part(cities, ',', 3) "3", split_part(cities, ',', 4) "4", split_part(cities, ',', 5) "5", split_part(cities, ',', 6) "6" from test 2. 문자열 분리 후 배열로 가져오기 with test as (select '서울,대구,부산,대전,인천,광주' cities) select string_to_array(cities, ',') fr..
공식 문서 참조 : https://www.postgresql.org/docs/9.3/functions-json.html OperatorRight Operand TypeDescriptionExample -> int Get JSON array element '[1,2,3]'::json->2 -> text Get JSON object field '{"a":1,"b":2}'::json->'b' ->> int Get JSON array element as text '[1,2,3]'::json->>2 ->> text Get JSON object field as text '{"a":1,"b":2}'::json->>'b' #> array of text Get JSON object at specified path '{"..