Search
Duplicate

ํ•œ๋ฐฉ ์ฟผ๋ฆฌ

๋ฐฐ๊ฒฝ

์ €ํฌ ๋ชจ๋ฝ ์„œ๋น„์Šค์—์„œ๋Š” โ€˜์—ญํ•  ์ •ํ•˜๊ธฐโ€™ ๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ๋ฐ์ผ๋ฆฌ ๋งˆ์Šคํ„ฐ, ์„œ๊ธฐ ๋“ฑ ๊ทธ๋ฃน ๋ณ„๋กœ ๋“ฑ๋กํ•œ ์—ญํ• ๋“ค์„ ๋žœ๋ค์œผ๋กœ ์ •ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์—ญํ•  ์ •ํ•˜๊ธฐ์˜ ํŽ˜์ด์ง€์—์„œ ์—ญํ•  ์ •ํ•˜๊ธฐ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๊ฐ ๊ทธ๋ฃน๋งˆ๋‹ค ์ €์žฅํ•œ ์—ญํ• ์„ ํŒ€์›๋“ค๊ณผ ๋งค์นญ์‹œ์ผœ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์—ญํ•  ์ •ํ•˜๊ธฐ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์กฐํšŒํ•˜๋Š” API ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด API ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์€ ๋ณต์žกํ•˜๊ธฐ๋•Œ๋ฌธ์— ๊ธฐ๋ณธ์ ์ธ JPA ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋Œ€์‹  query dsl ์„ ์ด์šฉํ•˜์—ฌ ํ•œ๋ฐฉ ์ฟผ๋ฆฌ๋กœ ๋ณด๋‚ด๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•œ๋ฐฉ ์ฟผ๋ฆฌ๋Š” ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ 2๊ฐœ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์žˆ์—ˆ๊ณ  ํ•ด๋‹น ์ฟผ๋ฆฌ๋กœ ์ธํ•ด ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š์•˜๋Š”๋ฐ์š”. ์ด ํ•œ๋ฐฉ ์ฟผ๋ฆฌ์— ๋Œ€ํ•ด ๋ถ„์„์„ ํ•ด๋ณด๊ณ  ๋” ๋‚˜์€ ๋ฐฉํ–ฅ์„ ์ฐพ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ƒํƒœ

ํ•ด๋‹น ๊ธฐ๋Šฅ์˜ erd ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์—ญํ•  ์ •ํ•˜๊ธฐ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์—ญํ•  ํžˆ์Šคํ† ๋ฆฌ ํ…Œ์ด๋ธ”์— ํด๋ฆญํ•œ ์‹œ๊ฐ„์ด ์ €์žฅ๋˜๊ณ , ๊ฐ๊ฐ์˜ ๋งค์นญ ๊ฒฐ๊ณผ๊ฐ€ ์—ญํ•  ์ •ํ•˜๊ธฐ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
์˜ค๋Š˜ ๋ณ€๊ฒฝํ•˜๋ ค๋Š” API ๋Š” ์—ญ๋Œ€ ๋งค์นญ ๊ธฐ๋ก์„ ์กฐํšŒํ•˜๋Š” API ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ์ €์žฅ์€ date time ํƒ€์ž…์œผ๋กœ ํ•˜์ง€๋งŒ, ๋น„์ฆˆ๋‹ˆ์Šค ์ •์ฑ… ์ƒ ์‘๋‹ต์—์„œ๋Š” ์ผ๋ณ„๋กœ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•œ ๋‚ ์งœ์— ์—ฌ๋Ÿฌ ๊ธฐ๋ก์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋‚ ์งœ์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ๋ฅผ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค.

๋กœ์ง

ํ•ด๋‹น ๋กœ์ง์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
1.
teamCode ๋กœ role ์กฐํšŒ
2.
์กฐํšŒํ•œ roleId ๊ฐ€ ํฌํ•จ๋œ role history ๋ชฉ๋ก์„ ์ผ๋ณ„๋กœ ๊ทธ๋ฃนํ™”/์ •๋ ฌํ•˜๊ณ  ์กฐํšŒ
3.
์กฐํšŒ๋œ role history ๊ฐ€ ํฌํ•จ๋œ role match result ๋ฅผ ์กฐํšŒ
List<RoleHistory> roleHistories = jpaQueryFactory.selectFrom(roleHistory) .join(roleHistory.matchResults).fetchJoin() .where(roleHistory.id.in( JPAExpressions.select(roleHistory.id.max()) .from(roleHistory) .groupBy(toLocalDate()) .orderBy(toLocalDate().desc()) .where(roleHistory.roleId.eq( JPAExpressions.select(role.id) .from(role) .where(role.teamCode.code.eq(teamCode)) )) )) .fetch();
Java
๋ณต์‚ฌ
2๊ฐœ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ํ•œ๋ฐฉ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆด ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
select rolehistor0_.id as id1_8_, rolehistor0_.date_time as date_tim2_8_, rolehistor0_.role_id as role_id3_8_, matchresul1_.role_history_id as role_his1_6_0__, matchresul1_.member_id as member_i2_6_0__, matchresul1_.role_name as role_nam3_6_0__ from role_history rolehistor0_ inner join role_match_result matchresul1_ on rolehistor0_.id=matchresul1_.role_history_id where rolehistor0_.id in ( select max(rolehistor2_.id) from role_history rolehistor2_ where rolehistor2_.role_id=( select role3_.id from role role3_ where role3_.team_code='edenee17' ) group by date(rolehistor2_.date_time) order by date(rolehistor2_.date_time) desc)
SQL
๋ณต์‚ฌ
using where ๋Š” ์Šคํ† ๋ฆฌ์ง€ ์—”์ง„์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ ๋’ค mysql ์—”์ง„์ด ์ถ”๊ฐ€ ์ฒดํฌ๋ฅผ ํ†ตํ•ด ํ•„ํ„ฐ๋งํ•œ๋‹ค๋Š” ์ •๋ณด์ž…๋‹ˆ๋‹ค.

date ์„ ํƒ ์ด์œ 

์ฟผ๋ฆฌ ์ œ์•ˆ

3.
์งง๊ฒŒ ์—ฌ๋Ÿฌ๋ฒˆ
select role0_.id as id1_5_, role0_.created_at as created_2_5_, role0_.updated_at as updated_3_5_, role0_.team_code as team_cod4_5_ from role role0_ where role0_.team_code='edenee1'
SQL
๋ณต์‚ฌ
select max(rolehistor0_.id) as col_0_0_ from role_history rolehistor0_ where rolehistor0_.role_id=17 group by dayofyear(rolehistor0_.date_time) order by dayofyear(rolehistor0_.date_time);
SQL
๋ณต์‚ฌ
select rolehistor0_.id as id1_8_, rolehistor0_.date_time as date_tim2_8_, rolehistor0_.role_id as role_id3_8_ from role_history rolehistor0_ where rolehistor0_.id in ( 50000 , 9350 )
SQL
๋ณต์‚ฌ
select matchresul0_.role_history_id as role_his1_6_0_, matchresul0_.member_id as member_i2_6_0_, matchresul0_.role_name as role_nam3_6_0_ from role_match_result matchresul0_ where matchresul0_.role_history_id in ( 50000 , 9350 )
SQL
๋ณต์‚ฌ