[JPA,Querydsl팁]칼럼별칭으로정렬하는방법(orderby, alias),스프링DataJPA,Querydsl강좌
// 칼럼별칭으로 OrderBy하기 위해
Path<Long> emp_rank = Expressions.numberPath(Long.class, "emp_rank");
Path<Long> dept_rank = Expressions.numberPath(Long.class, "dept_rank");
List<Tuple> rows = queryFactory
.select(emp.ename, emp.sal, emp.deptno,
SQLExpressions
.rank().over()
.orderBy(emp.sal.desc()).as(emp_rank), SQLExpressions
.denseRank().over()
.partitionBy(emp.deptno)
.orderBy(emp.sal.desc()).as(dept_rank)
)
.from(emp)
.orderBy(((ComparableExpressionBase<Long>) emp_rank).asc(), ((ComparableExpressionBase<Long>) dept_rank).asc())
.fetch();
select EMP.ENAME, EMP.SAL, EMP.DEPTNO, rank() over (order by EMP.SAL desc) emp_rank, dense_rank() over (partition by EMP.DEPTNO order by EMP.SAL desc) dept_rank
from EMP EMP
order by emp_rank asc, dept_rank asc
|-------|-----|-------|---------|----------|
|ename |sal |deptno |emp_rank |dept_rank |
|-------|-----|-------|---------|----------|
|KING |5000 |10 |1 |1 |
|FORD |3000 |20 |2 |1 |
|SCOTT |3000 |20 |2 |1 |
|JONES |2975 |20 |4 |2 |
|BLAKE |2850 |30 |5 |1 |
|CLARK |2450 |10 |6 |2 |
|ALLEN |1600 |30 |7 |2 |
|TURNER |1500 |30 |8 |3 |
|MILLER |1300 |10 |9 |3 |
|WARD |1250 |30 |10 |4 |
|MARTIN |1250 |30 |10 |4 |
|ADAMS |1100 |20 |12 |3 |
|JAMES |950 |30 |13 |5 |
|SMITH |800 |20 |14 |4 |
|-------|-----|-------|---------|----------|
댓글 없음:
댓글 쓰기