결과값
gubun | CODE_N | NO_P | F_DATE | M_DATE | M_WEEK | QTY | TYPE |
OLD | lg_sam | 521101-01 | 20180101 | 20190101 | 20190101 | 100 | A_TYPE |
NEW | lg_sam | 521101-02 | 20180102 | 20190107 | 20190102 | 100 | B_TYPE |
NEW | lg_sam | 521101-03 | 20180103 | 20190114 | 20190103 | 100 | C_TYPE |
NEW | lg_sam | 521101-04 | 20180104 | 20190114 | 20190103 | 50 | C_TYPE |
OLD | LG-SAM | 521102-01 | 20180105 | 20190107 | 20190102 | 100 | A_TYPE |
OLD | LG-SAM | 521102-02 | 20180106 | 20190108 | 20190102 | 100 | A_TYPE |
NEW | LG-SAM | 521102-03 | 20180107 | 20190114 | 20190103 | 50 | B_TYPE |
NEW | LG-SAM | 521102-03 | 20180107 | 20190121 | 20190104 | 50 | B_TYPE |
NEW | LG-SAM | 521102-04 | 20180108 | 20190121 | 20190104 | 100 | B_TYPE |
WITH t AS ( SELECT 'lg_sam' code_n, '521101-01' no_p, '20180101' f_date, '20190101' m_date, '20190101' m_week, 100 qty, 'A_TYPE' type FROM dual UNION ALL SELECT 'lg_sam', '521101-02', '20180102', '20190102', '20190101', 100, 'B_TYPE' FROM dual UNION ALL SELECT 'lg_sam', '521101-03', '20180103', '20190107', '20190102', 100, 'C_TYPE' FROM dual UNION ALL SELECT 'lg_sam', '521101-04', '20180104', '20190108', '20190102', 100, 'C_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-01', '20180105', '20190107', '20190102', 100,'A_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-02', '20180106', '20190108', '20190102', 100, 'A_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-03', '20180107', '20190109', '20190102', 100, 'B_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-04', '20180108', '20190114', '20190103', 100, 'B_TYPE' FROM dual ) , c AS ( SELECT 'lg_sam' code_n, '20190101' m_week, 150 qty,'A_TYPE' type FROM dual UNION ALL SELECT 'lg_sam', '20190102', 250, 'B_TYPE' FROM dual UNION ALL SELECT 'lg_sam', '20190103', 150, 'C_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '20190102', 350, 'A_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '20190103', 50, 'B_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '20190104', 150, 'B_TYPE' FROM dual ) , calendar AS ( SELECT '20190101' d_date, '20190101' d_week FROM dual UNION ALL SELECT '20190102', '20190101' FROM dual UNION ALL SELECT '20190103', '20190101' FROM dual UNION ALL SELECT '20190104', '20190101' FROM dual UNION ALL SELECT '20190105', '20190101' FROM dual UNION ALL SELECT '20190106', '20190101' FROM dual UNION ALL SELECT '20190107', '20190102' FROM dual UNION ALL SELECT '20190108', '20190102' FROM dual UNION ALL SELECT '20190109', '20190102' FROM dual UNION ALL SELECT '20190110', '20190102' FROM dual UNION ALL SELECT '20190111', '20190102' FROM dual UNION ALL SELECT '20190112', '20190102' FROM dual UNION ALL SELECT '20190113', '20190102' FROM dual UNION ALL SELECT '20190114', '20190103' FROM dual )
code 가 있는 곳마다 type 추가해 주면 됩니다.
혹시 달력 자료를 추가 안해서 원하는 결과가 안나온게 아닐런지?
WITH t AS ( SELECT 'lg_sam' code_n, '521101-01' no_p, '20180101' f_date, '20190101' m_date, '20190101' m_week, 100 qty, 'A_TYPE' type FROM dual UNION ALL SELECT 'lg_sam', '521101-02', '20180102', '20190102', '20190101', 100, 'B_TYPE' FROM dual UNION ALL SELECT 'lg_sam', '521101-03', '20180103', '20190107', '20190102', 100, 'C_TYPE' FROM dual UNION ALL SELECT 'lg_sam', '521101-04', '20180104', '20190108', '20190102', 100, 'C_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-01', '20180105', '20190107', '20190102', 100, 'A_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-02', '20180106', '20190108', '20190102', 100, 'A_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-03', '20180107', '20190109', '20190102', 100, 'B_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '521102-04', '20180108', '20190114', '20190103', 100, 'B_TYPE' FROM dual ) , c AS ( SELECT 'lg_sam' code_n, '20190101' m_week, 150 qty,'A_TYPE' type FROM dual UNION ALL SELECT 'lg_sam', '20190102', 250, 'B_TYPE' FROM dual UNION ALL SELECT 'lg_sam', '20190103', 150, 'C_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '20190102', 350, 'A_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '20190103', 50, 'B_TYPE' FROM dual UNION ALL SELECT 'LG-SAM', '20190104', 150, 'B_TYPE' FROM dual ) , calendar AS ( SELECT '20190101' d_date, '20190101' d_week FROM dual UNION ALL SELECT '20190102', '20190101' FROM dual UNION ALL SELECT '20190103', '20190101' FROM dual UNION ALL SELECT '20190104', '20190101' FROM dual UNION ALL SELECT '20190105', '20190101' FROM dual UNION ALL SELECT '20190106', '20190101' FROM dual UNION ALL SELECT '20190107', '20190102' FROM dual UNION ALL SELECT '20190108', '20190102' FROM dual UNION ALL SELECT '20190109', '20190102' FROM dual UNION ALL SELECT '20190110', '20190102' FROM dual UNION ALL SELECT '20190111', '20190102' FROM dual UNION ALL SELECT '20190112', '20190102' FROM dual UNION ALL SELECT '20190113', '20190102' FROM dual UNION ALL SELECT '20190114', '20190103' FROM dual UNION ALL SELECT '20190115', '20190103' FROM dual UNION ALL SELECT '20190116', '20190103' FROM dual UNION ALL SELECT '20190117', '20190103' FROM dual UNION ALL SELECT '20190118', '20190103' FROM dual UNION ALL SELECT '20190119', '20190103' FROM dual UNION ALL SELECT '20190120', '20190103' FROM dual UNION ALL SELECT '20190121', '20190104' FROM dual ) SELECT a.code_n , a.type , a.no_p , a.f_date , DECODE(gb, 'NEW', c.d_date, a.m_date) m_date , a.gb , a.m_week , SUM(a.use_qty) use_qty FROM (SELECT a.code_n, a.type, a.no_p, a.f_date, a.m_date , CASE WHEN a.m_week < b.m_week THEN 'NEW' ELSE 'OLD' END gb , CASE WHEN a.m_week < b.m_week THEN b.m_week ELSE a.m_week END m_week , LEAST( a.s_qty - b.s_qty + b.qty , b.s_qty - a.s_qty + a.qty , a.qty , b.qty ) use_qty FROM (SELECT code_n, type, no_p, f_date, m_date, m_week, qty , SUM(qty) OVER(PARTITION BY code_n, type ORDER BY m_date, f_date, no_p) s_qty FROM t ) a , (SELECT code_n, type, m_week, qty , SUM(qty) OVER(PARTITION BY code_n, type ORDER BY m_week) s_qty FROM c ) b WHERE a.code_n = b.code_n AND a.type = b.type AND a.s_qty > b.s_qty - b.qty AND b.s_qty > a.s_qty - a.qty ) a , (SELECT d_date , SUBSTR(d_week, 1, 4) yyyy , SUBSTR(d_week, 7, 2) ww , ROW_NUMBER() OVER(PARTITION BY SUBSTR(d_week, 1, 4), SUBSTR(d_week, 7, 2) ORDER BY d_date) rn FROM calendar ) c WHERE a.m_week LIKE c.yyyy || '__' || c.ww AND c.rn = 1 GROUP BY a.code_n, a.type, DECODE(a.gb, 'NEW', c.d_date, a.m_date), a.f_date, a.no_p, a.gb, a.m_week ORDER BY code_n, type, m_date, f_date, no_p ;