안녕하세요, 구루비에서 많은 도움을 받고 있는 개발자입니다
고민 끝에 문의 드립니다
A = CUS_MGR (고객관리)
- CUM_ID
- CUM_SEQ
B = CUS_DTL ( 품목내역)
- CUD_SEQ
- CUD_GRP_NO
- CUD_NO
- CUD_DTL_NO
- CUD_DATE
- CUD_DATE_SEQ
- CUD_VALUE
C = CUS_ITEM (품목명)
- CUI_ID
- CUI_NO
- CUI_NM
테이블(CUS_DTL)은 BigData(개인 10만 Row)입니다, RANK 함수 외에, 쿼리 속도 개선이 가능 할까요?
부탁드립니다
SELECT TO_CHAR(T.CUD_DATE, 'YYYYMMDDHH24MISS') , T.CUD_VALUE FROM ( SELECT B.CUD_DATE , B.CUD_VALUE , DENSE_RANK() OVER(PARTITION BY C.CUI_NM ORDER BY B.CUD_DATE DESC) AS RNK FROM CUS_MGR A , CUS_DTL B , CUS_ITEM C WHERE A.CUM_ID = '11111' AND B.CUD_SEQ = A.CUM_SEQ AND B.CUD_DATE >= TRUNC(SYSDATE) - 365 AND B.CUD_DATE < TRUNC(SYSDATE) + 1 AND C.CUI_ID = 'A01' AND C.CUI_NO = B.CUD_NO AND C.CUI_NM IN ('AA', 'AB', 'AC') ) T WHERE T.RNK = 1
죄송합니다, select에서 항목이 누락되었네요
SELECT T.CUD_NO , TO_CHAR(T.CUD_DATE, 'YYYYMMDDHH24MISS') , T.CUI_NM , T.CUD_VALUE FROM ( SELECT B.CUD_NO as CUD_NO , B.CUD_DATE as CUD_DATE , C.CUI_NM as CUI_NM , B.CUD_VALUE as CUD_VALUE , DENSE_RANK() OVER(PARTITION BY C.CUI_NM ORDER BY B.CUD_DATE DESC) AS RNK FROM CUS_MGR A , CUS_DTL B , CUS_ITEM C WHERE A.CUM_ID = '11111' AND B.CUD_SEQ = A.CUM_SEQ AND B.CUD_DATE >= TRUNC(SYSDATE) - 365 AND B.CUD_DATE < TRUNC(SYSDATE) + 1 AND C.CUI_ID = 'A01' AND C.CUI_NO = B.CUD_NO AND C.CUI_NM IN ('AA', 'AB', 'AC') ) T WHERE T.RNK = 1
-------------------- Output
CUD_NO | CUD_DATE | CUI_NM | CUD_VALUE | RNK |
120 | 2018-05-29 10:00:00 | AA | 100.25 | 1 |
120 | 2018-05-29 09:00:00 | AA | 95.75 | 2 |
120 | 2018-05-29 08:00:00 | AA | 99.17 | 3 |
120 | 2018-05-29 07:00:00 | AA | 102.05 | 4 |
131 | 2018-05-29 10:00:00 | AB | 170 | 1 |
132 | 2018-05-29 09:00:00 | AB | 150 | 2 |
132 | 2018-05-29 08:00:00 | AB | 180 | 3 |
129 | 2018-05-29 07:00:00 | AB | 160 | 4 |
151 | 2018-05-29 10:00:00 | AC | 100 | 1 |
162 | 2018-05-29 09:00:00 | AC | 95 | 2 |
162 | 2018-05-29 08:00:00 | AC | 120 | 3 |
149 |
2018-05-29 07:00:00 | AC | 110 | 4 |
------------------------------
SELECT * FROM cus_item WHERE cui_id = 'A01' AND cui_nm IN ('AA', 'AB', 'AC');
------------------------------ OutPut
CUS_ID | CUS_NO | CUS_NM |
A01 | 120 | AA |
A01 | 129 | AB |
A01 | 131 | AB |
A01 | 132 | AB |
A01 | 149 | AC |
A01 | 151 | AC |
A01 | 162 | AC |
입니다
SELECT * FROM (SELECT a.cui_nm , b.cud_no , b.cud_date , b.cud_value , RANK() OVER(PARTITION BY a.cui_nm ORDER BY b.cud_date DESC) rnk FROM (SELECT /*+ no_merge */ a.cum_seq , c.cui_no , c.cui_nm FROM cus_mgr a , cus_item c WHERE a.cum_id = '11111' AND c.cui_id = 'A01' AND c.cui_nm IN ('AA', 'AB', 'AC') ) a , cus_dtl b WHERE a.cum_seq = b.cud_seq AND a.cui_no = b.cud_no AND b.cud_date >= TRUNC(sysdate) - 365 AND b.cud_date < TRUNC(sysdate) + 1 ) WHERE rnk = 1 ;