โ์ด ์ฟผ๋ฆฌ๋ฅผ ์ฐ๊ฒ ๋ ์ด์ ๋...
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 ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.