728x90
oracle에서 rownum은 셀렉트된 데이터들에 번호를 매겨 그 데이터만을 가져오는것으로 알고 있다.
그래서 순서대로 데이터를 가져오기 위해서 사용하고 있었다.
그런데!! 어느순간 아무리 해도 절대 이해 할 수 없는 순서로 데이터를 가져오기 시작했다.
아래의 쿼리문 조건을 보자
1. rownum 이 10보다 이하인 데이터
2. orderdate가 2021-01-01 ~ 2021-05-06 일까지인 데이터
3. orderstatus가 구매확정인 데이터
를 조건으로 검색하고 orderdate를 기준으로 정렬한다 고 적어놓았다.
원래의 데이터 순서는 이렇다.
헌데, 아래의 조건으로 검색시 이렇게 나온다.
3월달부터 5월달 사이의 값이 없다!!! 5월달이 아니라 3월 ~ 4월달 데이터가 나와야 하는 것이다.
select d.ordernum, d.orderdate, d.orderprice, d.realpayment, c.settledate, c.settlecheck from ( select ordernum, orderstatus, settlecheck, settledate from ( select productnum from product where userid='ghdrlfehd' ) a join orderdetail b on a.productnum = b.productnum) c join ordertbl d on c.ordernum = d.ordernum where rownum <= 10 and orderdate BETWEEN '2021-01-01' and '2021-05-06' and orderstatus='구매확정' order by orderdate asc
몇시간동안 수십번을 테스트해본 결과 답이 없었다.
갑자기 문득 생각난 방법으로 테스트해보기로 했다.
rownum과 order by 를 지워보았더니 아래와 같이 나왔다.
select d.ordernum, d.orderdate, d.orderprice, d.realpayment, c.settledate, c.settlecheck from ( select ordernum, orderstatus, settlecheck, settledate from ( select productnum from product where userid='ghdrlfehd' ) a join orderdetail b on a.productnum = b.productnum) c join ordertbl d on c.ordernum = d.ordernum where orderdate BETWEEN '2021-01-01' and '2021-05-06' and orderstatus='구매확정'
즉, rownum은 order by (정렬)이 되기 전에 명령을 먼저 수행하고 정렬한다는 것이다.
그렇다면 어떻게 해결할까?
답은
1. 정렬해서 계산한다.
2. 다음에 계산한 데이터를 한번 감싸서 rownum을 구하면 되겠다.
아래는 게시판과 같은 효과를 주는 sql문을 만들어 사용했다.
게시판은 페이지 이동시의 가져오는 데이터가 다르게 나와야 하기 때문에 2번을 감싸서 계산해야 한다.
select ordernum, to_char(orderdate, 'YYYY-MM-DD') orderdate, orderprice, realpayment, to_char(settledate, 'YYYY-MM-DD') settledate, settlecheck from ( select ordernum, orderdate, orderprice, realpayment, settledate, settlecheck from ( select d.ordernum, d.orderdate, d.orderprice, d.realpayment, c.settledate, c.settlecheck from ( select ordernum, orderstatus, settlecheck, settledate from ( select productnum from product where userid='ghdrlfehd' ) a join orderdetail b on a.productnum = b.productnum) c join ordertbl d on c.ordernum = d.ordernum where orderdate BETWEEN '2021-01-01' and '2021-05-06' and orderstatus='구매확정' order by orderdate asc) where rownum <= 10 order by orderdate desc, ordernum desc) where rownum <=10 order by orderdate asc;
728x90
'Infrastructure > Database' 카테고리의 다른 글
MySql Database 생성, 유저 생성 (0) | 2021.09.22 |
---|---|
oracle 스케쥴러 작성하여, 자동으로 반복 일정(지정한 쿼리 or 프로시저)을 실행하기 (0) | 2021.05.19 |
oracle 이전글, 다음글 구하기 (0) | 2021.04.19 |
oracle 커서 (0) | 2021.03.01 |
oracle 트리거 (0) | 2021.03.01 |