728x90
GroupBy
๋ฐ์ดํฐ๋ฅผ ๊ทธ๋ฃนํํ์ฌ ์กฐํํ๊ธฐ ์ํด GroupBy๋ฅผ ์ฌ์ฉํฉ๋๋ค.
having์ ํตํด ๊ทธ๋ฃนํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ ํํ ์ ์์ต๋๋ค.
์ ์ ์ฝ๋
@BeforeEach
fun before() {
val teamA = Team(name = "teamA")
val teamB = Team(name = "teamB")
em.persist(teamA)
em.persist(teamB)
val members = listOf(
Member(username = "member1", age = 10, team = teamA),
Member(username = "member2", age = 20, team = teamA),
Member(username = "member3", age = 30, team = teamB),
Member(username = "member4", age = 40, team = teamB),
Member(username = "member5", age = 50, team = teamA),
Member(username = "member6", age = 60, team = teamA),
Member(username = "member7", age = 70, team = teamB),
Member(username = "member8", age = 80, team = teamB),
Member(username = "member9", age = 90, team = teamA),
Member(username = "member10", age = 100, team = teamA),
Member(username = "member11", age = 110, team = teamB),
Member(username = "member12", age = 120, team = teamB),
)
members.forEach { em.persist(it) }
em.flush()
em.clear()
}
์ฌ์ฉ
/**
* ํ์ ์ด๋ฆ(select)๊ณผ ๊ฐ ํ์ ํ๊ท ์ฐ๋ น(select)
*/
@Test
fun testGroupBy() {
//given
val fetch = query
.select(team.name, member.age.avg())
.from(member)
.join(member.team, team)
.groupBy(team.name)
.fetch()
val teamA = fetch[0]
val teamB = fetch[1]
assertThat(teamA.get(team.name)).isEqualTo("teamA")
assertThat(teamA.get(member.age.avg())).isEqualTo(55.0)
assertThat(teamB.get(member.age.avg())).isEqualTo(75.0)
assertThat(teamB.get(team.name)).isEqualTo("teamB")
}
having() ์์
...
.groupBy(team.name)
.having(member.age.loe(10))
...
Reference
728x90
'๐๏ธ Spring > QueryDsl' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[QueryDSL] ์๋ธ ์ฟผ๋ฆฌ (0) | 2021.12.23 |
---|---|
[QueryDSL] ์กฐ์ธ, ํ์น์กฐ์ธ (0) | 2021.12.23 |
[QueryDSL] ์ง๊ณ ํจ์(count, sum, avg, max, min) (0) | 2021.12.23 |
[QueryDSL] ์ ๋ ฌ - orderBy() (0) | 2021.12.23 |
[QueryDSL] ๊ฒฐ๊ณผ ์กฐํ (0) | 2021.12.23 |