[JPA,Querydsl팁]칼럼별칭으로정렬하는방법(orderby,
alias),스프링DataJPA,Querydsl강좌
// 칼럼별칭으로 OrderBy하기 위해
Path<Long> emp_rank = __EXPRESSION__s.numberPath(Long.class, "emp_rank");
Path<Long> dept_rank = __EXPRESSION__s.numberPath(Long.class, "dept_rank");
List<Tuple> rows = queryFactory
.select(emp.ename, emp.sal, emp.deptno,
SQL__EXPRESSION__s
.rank().over()
.orderBy(emp.sal.desc()).as(emp_rank), SQL__EXPRESSION__s
.denseRank().over()
.partitionBy(emp.deptno)
.orderBy(emp.sal.desc()).as(dept_rank)
)
.from(emp)
.orderBy(((Comparable__EXPRESSION__Base<Long>) emp_rank).asc(), ((Comparable__EXPRESSION__Base<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 |
|-------|-----|-------|---------|----------|
// 칼럼별칭으로 OrderBy하기 위해
Path<Long> emp_rank = __EXPRESSION__s.numberPath(Long.class, "emp_rank");
Path<Long> dept_rank = __EXPRESSION__s.numberPath(Long.class, "dept_rank");
List<Tuple> rows = queryFactory
.select(emp.ename, emp.sal, emp.deptno,
SQL__EXPRESSION__s
.rank().over()
.orderBy(emp.sal.desc()).as(emp_rank), SQL__EXPRESSION__s
.denseRank().over()
.partitionBy(emp.deptno)
.orderBy(emp.sal.desc()).as(dept_rank)
)
.from(emp)
.orderBy(((Comparable__EXPRESSION__Base<Long>) emp_rank).asc(), ((Comparable__EXPRESSION__Base<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 |
|-------|-----|-------|---------|----------|
댓글 없음:
댓글 쓰기