티스토리 뷰

Skill/postgreSQL

postgresql JSON 배열 조회

진열사랑 2024. 11. 1. 12:15

[{구간ID:1, 시작일:1, 종료일:15, 주기유형코드:3, 주기기간:0, 구간정산일:99}, // 당월 말일 정산
{구간ID:2, 시작일:16, 종료일:99, 주기유형코드:3, 주기기간:1, 구간정산일:20} // 다음달 20일 정산 ]

SELECT ff.cid, ff.cpid, ff.pymt_cd, ff.aply_st_ymd
, (rai->>'sn')::NUMBER AS sn
, (rai->>'payStDay')::NUMBER AS payStDay
, (rai->>'payFnshDay')::NUMBER AS payFnshDay
, (rai->>'adjItvlTypCd')::NUMBER AS adjItvlTypCd
, (rai->>'adjItvlTrm')::NUMBER AS adjItvlTrm
, (rai->>'adjItvlDay')::NUMBER AS adjItvlDay
FROM sppdb.tb_cid_gnr_adj_fee ff
CROSS JOIN json_array_elements(ff.rng_adj_info) AS rai
WHERE rng_adj_info IS NOT NULL AND aply_st_ymd = '2024-11-01';

cid cpid pymt_cd aply_st_ymd sn paystday payfnshday adjitvltypcd adjitvltrm adjitvlday
honda00001 CMSCPID cmsbank 2024-11-01 1 1 15 3 0 99
honda00001 CMSCPID cmsbank 2024-11-01 2 16 99 3 1 20


----------------
JSON ARRY사용법
SELECT a.ymd, a.cid, a.pymt_cd, a.cpid, a.adj_itvl_typ_cd, a.adj_itvl_trm
, (b->>'payStDay')::smallint pay_st_day
, (b->>'payFnshDay')::smallint pay_fnsh_day
, (b->>'adjItvlTypCd')::smallint json_adj_itvl_typ_cd
, (b->>'adjItvlTrm')::smallint json_adj_itvl_trm
, (b->>'adjItvlDay')::smallint json_adj_itvl_day
, a.rng_adj_info::varchar rng_adj_info
FROM spsdb.tb_clnt_pymt_bydd_cntrc_info a
LEFT
JOIN json_array_elements(a.rng_adj_info) b ON EXTRACT(DAY FROM '20241113'::date) BETWEEN (b->>'payStDay')::smallint AND (b->>'payFnshDay')::smallint
WHERE a.ymd = CURRENT_DATE - 1
AND a.pymt_cd = 'cmsbank'

'Skill > postgreSQL' 카테고리의 다른 글

postgresql string_to_array  (1) 2024.11.29
postgresql CTE  (0) 2024.10.11
PostgreSQL) Sequence 생성, 활용, 초기화  (0) 2024.10.07
postgresql] column명과 type조회  (0) 2024.09.20
postgresql DDL  (0) 2024.08.23
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함