DBMS/PostgreSQL

[ PostgreSQL ] mybatis์—์„œ postgresql์˜ ํ”ผ๋ฒ— (crosstab) ๊ณผ ๋™์ ์ฟผ๋ฆฌ๋ฅผ ํ•จ๊ป˜ ์จ๋ณด๋ ค๋Š” ๋…ธ๋ ฅ (๊ฒฐ๊ณผ ์„ฑ๊ณต)

seulye 2023. 10. 27. 15:57

 

โ“์ด ์ฟผ๋ฆฌ๋ฅผ ์“ฐ๊ฒŒ ๋œ ์ด์œ ๋Š”... 

https://careerly.co.kr/qnas/5171?utm_campaign=user-share

 

PostgreSQL ํŠน์ • ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ์„œ ๋™์  ์ปฌ๋Ÿผ ์„ค์ • ๋ฐฉ๋ฒ•

ํ˜„์žฌ ํ†ต๊ณ„ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ํŠน์ • ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ์„œ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์˜ ์ปฌ๋Ÿผ์ด ๋™์ ์œผ๋กœ ๋ณ€ํ–ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค! ์˜ˆ๋ฅผ ๋“ค๋ฉด, ํ•™์ƒ๋ณ„๋กœ ์„ฑ์ ์„ ๋ฝ‘๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑ...

careerly.co.kr

์—ฌ๊ธฐ์— ์ ํ˜€ ์žˆ๋‹ค. ์งˆ๋ฌธํ•  ๋•Œ ํž˜๋“  ๊ฒƒ ์ค‘ ํ•˜๋‚˜๋Š”.. ํšŒ์‚ฌ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋…ธ์ถœํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋Ÿฐ์‹์œผ๋กœ ๋น„์œ ๋ฅผ ํ•ด์„œ ์„ค๋ช…ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋จธ๋ฆฌ ํ•œ๋ฒˆ ๊ตด๋ ค์ค˜์•ผํ•จ ใ… _ใ…  

 

๋‹ต๋ณ€์ด ํฐ ๋„์›€์ด ๋˜์—ˆ์ง€๋งŒ! ํšŒ์‚ฌ ์‚ฌ์ˆ˜๋ถ„์ด crosstab์„ ์ด์šฉํ•œ ์ฟผ๋ฆฌ๋ฅผ ์˜ˆ์‹œ๋กœ ์ฃผ์…จ๋Š”๋ฐ, ๊ฝค ํ• ๋งŒํ•œ๋ฐ..? ํ•ด์„œ ํ‚ต๊ณ ์ž‰์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค๋Š” ์ด์•ผ๊ธฐ~~! ํ•˜์ง€๋งŒ ๊ธธ์ด ๊ต‰์žฅํžˆ ํ—˜๋‚œํ–ˆ๋Š”๋ฐ...๐Ÿค” ๊ทธ ์—ฌ์ •์„ ๋Œ€์ถฉ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค. ๊ผฌ๋ฐ• ํ•˜๋ฃจ๊ฐ€ ๊ฑธ๋ ธ๊ธฐ ๋•Œ๋ฌธ์— ์‹œํ–‰์ฐฉ์˜ค๊ฐ€ ๋งŽ์•˜์ง€๋งŒ, ๋ชจ๋“  ๋‚ด์šฉ์„ ์ ์–ด๋‘˜ ์ˆ˜ ์—†์—ˆ๋‹ค. 

 

๐Ÿ’กํ”ผ๋ฒ—

์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋ฉด ์ข‹๊ฒ ์ง€๋งŒ, ์•„๋ž˜์˜ ๊ธฐ๋ก์ด ๋” ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋‹จ์€ ์ƒ๋žต์„ ํ•˜๋„๋ก ํ•œ๋‹ค. 

https://velog.io/@mstar228/PostgreSQL-CrosstabPivot

 

PostgreSQL Crosstab(Pivot)

crosstab ์‚ฌ์šฉ ์ •๋ฆฌ

velog.io

์ด ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ํ•ด๋„ ์ข‹์„๋“ฏ!!

 

SELECT *
FROM   crosstab(
   'SELECT section, status, ct
    FROM   tbl
    ORDER  BY 1,2'  -- needs to be "ORDER BY 1,2" here
   ) AS ct ("Section" text, "Active" int, "Inactive" int);

์œ„ ๋ธ”๋กœ๊ทธ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉฐ, ๋‚ด๊ฐ€ ๋‹จ์ˆœํ•˜๊ฒŒ ์ดํ•ดํ•œ๊ฑธ ๋งํ•˜๋ฉด! 

SELECT section, status, ct ์ด ๋ถ€๋ถ„์—์„œ ๋’ค์—์„œ 2๋ฒˆ์งธ 'status'๋กœ ๋ฌถ์ด๊ณ  ๋ฐ์ดํ„ฐ์—๋Š” ๋งˆ์ง€๋ง‰ 'ct'๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค. 

์•”ํŠผ, ์—ฌ์ฐจ์ €์ฐจ์—์„œ ์ฟผ๋ฆฌ๋Š” ์ž‘์„ฑ ์™„๋ฃŒ ํ–ˆ๋‹ค. DBeaber์—์„œ ์ž˜ ๋Œ์•„๊ฐ”๋‹ค. 

 

ํ•˜์ง€๋งŒ... mybatis์—์„œ crosstab ์•ˆ์— ๋™์ ์ฟผ๋ฆฌ๋ฅผ ๋„ฃ์œผ๋ฉด์„œ ํ—˜๋‚œํ•ด์กŒ๋‹ค. 

 

๐Ÿ”์ฒซ ๋ฒˆ์งธ ์˜ค๋ฅ˜

Could not set parameters for mapping: ParameterMapping{property='__frch_item_0.cdId', mode=IN, javaType=class java.lang.Integer, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.

 

์ด ์˜ค๋ฅ˜๋ฅผ ์ง๋ฉดํ–ˆ๋‹ค. parameter๊ฐ€ null๋กœ ๋“ค์–ด๊ฐ„๋‹ค๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ.. 

<if test="agentId != null and agentId != ''">
			  and pas.agent_id = #{agentId}
			  </if>

์ด๋Ÿฐ์‹์œผ๋กœ ๋˜‘๊ฐ™์ด ๋™์ ์ฟผ๋ฆฌ๋ฅผ ๋„ฃ์—ˆ๋Š”๋ฐ ์™œ ๋‚˜์˜ค์ง€ ์•Š๋Š” ๊ฒƒ์ธ๊ฐ€! 

 

 

<if test="agentId != null and agentId != ''">
			  and pas.agent_id = ${agentId}
			  </if>

๊ทธ๋ž˜์„œ ์ด๊ฑธ๋กœ ๋ณ€๊ฒฝํ•ด๋ดค๋‹ค. ํ•˜์ง€๋งŒ pas.agent_id๋Š” char๊ณ , agentId๋Š” ์ž๊พธ ์ˆซ์ž๋กœ ๋„˜์–ด๊ฐ€๋”๋ผ...

 

<if test="agentId != null and agentId != ''">
			  and pas.agent_id = ''${agentId}''
			  </if>

์ด๋ ‡๊ฒŒ ํ•ด์ฃผ์—ˆ๋”๋‹ˆ ์ž˜ ๋˜์—ˆ๋‹ค! ใ…Žใ…Ž crosstab ๊ด„ํ˜ธ ๋‚ด๋ถ€๋Š” ์ž‘์€ ๋”ฐ์˜ดํ‘œ๋กœ ์”Œ์›Œ์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด์ด ์ž˜ ๋“ค์–ด๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž‘์€ ๋”ฐ์˜ดํ‘œ๋ฅผ 2๊ฐœ์”ฉ ์จ์ค˜์•ผํ–ˆ๋‹ค. 

 

+ ์ฐธ๊ณ ๋กœ ๋™์ ์ฟผ๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด ์•ˆ ๋˜์–ด์„œ, ์กฐํšŒ ์กฐ๊ฑด์ด ๋“ค์–ด๊ฐ€๋Š” select์ ˆ์„ with์ ˆ๋กœ ๋นผ๋ฒ„๋ฆฌ๊ณ , crosstab์—์„œ๋Š” ๊ฑธ๋Ÿฌ์ง„ ๋ฐ์ดํ„ฐ๋“ค์„ ํ† ๋Œ€๋กœ selectํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ! with์ ˆ์˜ ๊ฐ€์ƒ ํ…Œ์ด๋ธ”์„ crosstab์—์„œ ์ธ์ง€๋ฅผ ๋ชปํ–ˆ๋‹ค. 

 

 

๐Ÿ”๋‘ ๋ฒˆ์งธ ๋ฌธ์ œ

์ฟผ๋ฆฌ ๋Œ๋ ธ๋Š”๋ฐ ์ž๊พธ ํ•œ์ค„ ๋ฐ–์— ์•ˆ ๋‚˜์˜ค๋Š” ํ˜„์ƒ์ด ์žˆ์—ˆ๋‹ค.. 

https://stackoverflow.com/questions/48680978/postgres-crosstab-text-text-cascading-within-group

 

Postgres crosstab (text, text) cascading within group

Table schema DROP TABLE bla; CREATE TABLE bla (id INTEGER, city INTEGER, year_ INTEGER, month_ INTEGER, val INTEGER); Data INSERT INTO bla VALUES(1, 1, 2017, 1, 10); INSERT INTO bla VALUES(2, 1,...

stackoverflow.com

์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ์—์„œ ๊ฐ™์€ ์ƒํ™ฉ์˜ ๊ฒŒ์‹œ๊ธ€์ด ์žˆ์—ˆ๊ณ , ๋”ฐ๋ผํ•ด๋ณด๋‹ˆ ์ž˜ ๋‚˜์™”๋‹ค!

select dense_rank() over (order by agent_id, job_id)::int as row_name

์ด๋Ÿฐ์‹์œผ๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ , 

AS ๋’ค์— rowname integer ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.