A 테이블
SELECT '201601' AS yyyymm UNION ALL SELECT '201602' AS yyyymm UNION ALL SELECT '201603' AS yyyymm UNION ALL SELECT '201604' AS yyyymm UNION ALL SELECT '201605' AS yyyymm UNION ALL SELECT '201606' AS yyyymm UNION ALL SELECT '201607' AS yyyymm UNION ALL SELECT '201608' AS yyyymm UNION ALL SELECT '201609' AS yyyymm UNION ALL SELECT '201610' AS yyyymm UNION ALL SELECT '201611' AS yyyymm UNION ALL SELECT '201612' AS yyyymm
B 테이블
SELECT '20160101' AS change_date, '기린' AS name UNION ALL SELECT '20160401' AS change_date, '사자' AS name UNION ALL SELECT '20160601' AS change_date, '코끼리' AS name UNION ALL SELECT '20160801' AS change_date, '원숭이' AS name
이 있습니다.
A 와 B 테이블을 조인해서
yyyymm | name |
201601 | 기린 |
201602 | 기린 |
201603 | 기린 |
201604 | 사자 |
201605 | 사자 |
201606 | 코끼리 |
201607 | 코끼리 |
201608 | 원숭이 |
201609 | 원숭이 |
201610 | 원숭이 |
201611 | 원숭이 |
201612 | 원숭이 |
위같은 결과값을 뽑아내고 싶은데.. 어떻게 조인을 해야하나요??
오! 감사합니다!
혹시 B 테이블이
SELECT '20160101' AS change_date, '기린' AS name UNION ALL SELECT '20160401' AS change_date, '사자' AS name UNION ALL SELECT '20160615' AS change_date, '코끼리' AS name UNION ALL -- 15일로 변경 SELECT '20160801' AS change_date, '원숭이' AS name
이런 모양일 때
yyyymm | name | |
201601 | 기린 | |
201602 | 기린 | |
201603 | 기린 | |
201604 | 사자 | |
201605 | 사자 | |
201606 | 사자 | <-- 추가됨 |
201606 | 코끼리 | |
201607 | 코끼리 | |
201608 | 원숭이 | |
201609 | 원숭이 | |
201610 | 원숭이 | |
201611 | 원숭이 | |
201612 | 원숭이 |
위와같이 나오는 방법이 있을까요?? 첫번째 질문을 자세하게 못드려서 죄송합니다..ㅠ 방법이 달라져야 할듯해서요..
WITH a AS ( SELECT '201601' yyyymm UNION ALL SELECT '201602' UNION ALL SELECT '201603' UNION ALL SELECT '201604' UNION ALL SELECT '201605' UNION ALL SELECT '201606' UNION ALL SELECT '201607' UNION ALL SELECT '201608' UNION ALL SELECT '201609' UNION ALL SELECT '201610' UNION ALL SELECT '201611' UNION ALL SELECT '201612' ) , b AS ( SELECT '20160101' change_date, '기린' name UNION ALL SELECT '20160401', '사자' UNION ALL SELECT '20160615', '코끼리' UNION ALL SELECT '20160801', '원숭이' ) SELECT a.yyyymm , b.name FROM a INNER JOIN (SELECT change_date sdt , LEAD(change_date, 1, '99991231') OVER(ORDER BY change_date) edt , name FROM b ) b ON b.sdt <= CONCAT(a.yyyymm, '31') AND b.edt > CONCAT(a.yyyymm, '01') ORDER BY a.yyyymm, b.sdt ;