SALE 테이블(판매)
제품코드 |
판매일 |
판매시간 |
판매자 |
A |
20201020 |
1300 |
홍길동 |
B |
20201021 |
0910 |
마징가 |
C |
20201021 |
1435 |
홍길동 |
MAKE 테이블(생산)
제품코드 |
생산일자 |
생산자 |
기타 |
A |
2020-09-30 12:10:18 |
AAA |
|
A |
2020-10-20 10:10:18 |
BBB |
|
A |
2020-10-22 13:10:18 |
AAA |
|
B |
2020-09-28 12:10:18 |
CCC |
|
B |
2020-10-15 15:10:18 |
AAA |
|
B |
2020-10-20 08:10:18 |
DDD |
|
B |
2020-10-30 16:10:18 |
CCC |
|
C |
2020-09-20 12:10:18 |
EEE |
|
C |
2020-10-21 13:10:18 |
ZZZ |
|
C |
2020-10-25 18:20:18 |
AAA |
|
SALE 테이블(판매) 기준으로 판매일(문자형) + 판매시간(문자형) 이 MAKE 테이블(생산)의 생산일자(날짜형)를 넘지 않는 최근 생산일자에 해당하는 생산자를 가져오고 싶습니다.
결과는 아래와 같아야 합니다.
제품코드 |
판매자 |
생산자 |
판매일 |
A |
홍길동 |
BBB |
20201020 |
B |
마징가 |
DDD |
20201021 |
C |
홍길동 |
ZZZ |
20201021 |
초보라 어찌해야 될지 모르겠네요..
고수님들 도와주세요.. ㅠ
감사합니다.
결과는 맞는데 제대로 이해한건지 모르겠네요. 생산일자를 넘지 않는 최근 생산일자라고 하셨는데 헷갈리는군요.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | with t1 as ( select 'A' code, '20201020' dt, '1300' ti, '홍길동' nm from dual union all select 'B' , '20201021' , '0910' , '마징가' from dual union all select 'C' , '20201021' , '1435' , '홍길동' from dual ), t2 as ( select 'A' code, to_date( '2020-09-30 12:10:18' , 'YYYY-MM-DD HH24:MI:SS' ) dt, 'AAA' nm from dual union all select 'A' , to_date( '2020-10-20 10:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'BBB' from dual union all select 'A' , to_date( '2020-10-22 13:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'AAA' from dual union all select 'B' , to_date( '2020-09-28 12:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'CCC' from dual union all select 'B' , to_date( '2020-10-15 15:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'AAA' from dual union all select 'B' , to_date( '2020-10-20 08:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'DDD' from dual union all select 'B' , to_date( '2020-10-30 16:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'CCC' from dual union all select 'C' , to_date( '2020-09-20 12:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'EEE' from dual union all select 'C' , to_date( '2020-10-21 13:10:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'ZZZ' from dual union all select 'C' , to_date( '2020-10-25 18:20:18' , 'YYYY-MM-DD HH24:MI:SS' ), 'AAA' from dual ) select * from ( select t1.code, t1.nm t1_nm, t2.nm t2_nm, t1.dt, row_number() over (partition by t1.code order by abs (t2.dt - to_date (t1.dt||t1.ti, 'YYYYMMDDHH24MI' ))*24*60) rn from t1, t2 where t1.code = t2.code ) where rn=1 |