-
[Spring] querydsl - 집합Spring 2022. 1. 3. 16:42
집합 함수
@Test public void aggregation() { List<Tuple> result = queryFactory .select( member.count(), member.age.sum(), member.age.avg(), member.age.max(), member.age.min() ) .from(member) .fetch(); Tuple tuple = result.get(0); assertThat(tuple.get(member.count())).isEqualTo(4); assertThat(tuple.get(member.age.sum())).isEqualTo(100); assertThat(tuple.get(member.age.avg())).isEqualTo(25); assertThat(tuple.get(member.age.max())).isEqualTo(40); assertThat(tuple.get(member.age.min())).isEqualTo(10); }
- tuple로 반환해서 Q클래스로 get 가능
GroupBy 사용
/** * 팀의 이름과 각 팀의 평균 연령을 구해라 */ @Test public void group() throws Exception{ //given List<Tuple> result = queryFactory .select(team.name, member.age.avg()) .from(member) .join(member.team, team) .groupBy(team.name) .fetch(); Tuple teamA = result.get(0); Tuple teamB = result.get(1); assertThat(teamA.get(team.name)).isEqualTo("teamA"); assertThat(teamA.get(member.age.avg())).isEqualTo(15); assertThat(teamB.get(team.name)).isEqualTo("teamB"); assertThat(teamB.get(member.age.avg())).isEqualTo(35); //when //then }
'Spring' 카테고리의 다른 글
[Spring] querydsl - paging (0) 2022.01.03 [Spring] @RequestParam 와 @PathVariable (0) 2022.01.03 [Spring] querydsl - 정렬 (0) 2022.01.03 [Spring] querydsl - 결과 조회 (0) 2022.01.03 [Spring] querydsl - 검색 조건 쿼리 (0) 2022.01.03