728x90
์ง๊ณ (count, sum, avg, max, min)
์ง๊ณ ํจ์๋ ๊ทธ๋ฃน ๋ณ๋ก ๊ทธ๋ฃนํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํฉ๋๋ค.
- COUNT() : ์ด ๊ฐ์
- SUM() : ํฉ
- AVG() : ํ๊ท
- MAX() : ์ต๋๊ฐ
- MIN() : ์ต์๊ฐ
์ฌ์ฉ ์์
์ ์ ์ฝ๋
@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()
}
์ฌ์ฉ
@Test
fun testGroup() {
val result = query
.select(
member.count(), // member๋ QMember.member
member.age.sum(),
member.age.avg(),
member.age.max(),
member.age.min(),
)
.from(member)
.fetchOne()
result!!
assertThat(result.get(member.count())).isEqualTo(12)
assertThat(result.get(member.age.sum())).isEqualTo(780)
assertThat(result.get(member.age.avg())).isEqualTo(65.0)
assertThat(result.get(member.age.max())).isEqualTo(120)
assertThat(result.get(member.age.min())).isEqualTo(10)
}
Select ์ ์ ์์ ๊ฐ์ด ์ฌ๋ฌ ํ์ ์ ์กฐํํ๋ ๊ฒฝ์ฐ QueryDsl์์ ์ ๊ณตํ๋ Tuple๋ก ์กฐํ๋ฉ๋๋ค.
ํ๋๋ง ์กฐํ ์์๋, List<Integer>์ ๊ฐ์ด ์กฐํํ ํ์ ์ List๋ก ์กฐํ๋ฉ๋๋ค.
Tuple์ get()๋ฉ์๋์ ์ธ์๋ก SELECT ์ ์์ ์ํํ๋ Expression์ ๊ทธ๋๋ก ์ฌ์ฉํด์ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
Reference
728x90
'๐๏ธ Spring > QueryDsl' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[QueryDSL] ์กฐ์ธ, ํ์น์กฐ์ธ (0) | 2021.12.23 |
---|---|
[QueryDSL] GroupBy (0) | 2021.12.23 |
[QueryDSL] ์ ๋ ฌ - orderBy() (0) | 2021.12.23 |
[QueryDSL] ๊ฒฐ๊ณผ ์กฐํ (0) | 2021.12.23 |
[QueryDSL] ๊ฒ์ ์กฐ๊ฑด ์ฟผ๋ฆฌ (3) | 2021.12.23 |