by 이카루스 [SQL Query] LAG LEAD oracle [2020.01.30 17:35:46]
안녕하세요 매일 현재재고 합계를 위해 LAG, LEAD 함수를 써도 신통치 않아 염치없이 도움을 요청합니다.. ㅠㅠ -- 소스 --- with t1 as ( select 'aaaa1111' as 상품코드, '20191225' as 일자, 0 as 현재재고, 0 as 입고, 0 as 출고 from dual union all select 'aaaa1111' , '20191226' , 0 , 10, 0 from dual union all select 'aaaa1111' , '20191227' , 0 , 30, 10 from dual union all select 'aaaa1111' , '20191228' , 0 , 0, 10 from dual union all select 'aaaa1111' , '20191229' , 0 , 0, 10 from dual union all select 'aaaa1111' , '20191230' , 0 , 0, 10 from dual union all select 'aaaa1111' , '20191231' , 0 , 5, 0 from dual union all select 'aaaa1111' , '20200101' , 0 , 5, 0 from dual union all select 'aaaa1111' , '20200102' , 0 , 5, 0 from dual union all select 'aaaa1111' , '20200103' , 0 , 5, 5 from dual union all select 'aaaa1111' , '20200103' , 0 , 0, 5 from dual ) select * from t1; ---타켓 / 현재재고합산 결과 ---- with t2 as ( select 'aaaa1111' as 상품코드, '20191225' as 일자, 0 as 현재재고, 0 as 입고, 0 as 출고 from dual union all select 'aaaa1111' , '20191226' , 10 , 10, 0 from dual union all select 'aaaa1111' , '20191227' , 30 , 30, 10 from dual union all select 'aaaa1111' , '20191228' , 20 , 0, 10 from dual union all select 'aaaa1111' , '20191229' , 10 , 0, 10 from dual union all select 'aaaa1111' , '20191230' , 0 , 0, 10 from dual union all select 'aaaa1111' , '20191231' , 5 , 5, 0 from dual union all select 'aaaa1111' , '20200101' , 10 , 5, 0 from dual union all select 'aaaa1111' , '20200102' , 15 , 5, 0 from dual union all select 'aaaa1111' , '20200103' , 15 , 5, 5 from dual union all select 'aaaa1111' , '20200103' , 10 , 0, 5 from dual ) select * from t2;
20100103이 두 개인데 일자별 순번 컬럼은 없나요?
with t1 as ( select 'aaaa1111' as 상품코드, '20191225' as 일자, 0 as 현재재고, 0 as 입고, 0 as 출고 from dual union all select 'aaaa1111' , '20191226' , 0 , 10, 0 from dual union all select 'aaaa1111' , '20191227' , 0 , 30, 10 from dual union all select 'aaaa1111' , '20191228' , 0 , 0, 10 from dual union all select 'aaaa1111' , '20191229' , 0 , 0, 10 from dual union all select 'aaaa1111' , '20191230' , 0 , 0, 10 from dual union all select 'aaaa1111' , '20191231' , 0 , 5, 0 from dual union all select 'aaaa1111' , '20200101' , 0 , 5, 0 from dual union all select 'aaaa1111' , '20200102' , 0 , 5, 0 from dual union all select 'aaaa1111' , '20200103' , 0 , 5, 5 from dual union all select 'aaaa1111' , '20200103' , 0 , 0, 5 from dual ) select 상품코드 , 일자 , sum(입고) OVER(PARTITION BY 상품코드 ORDER BY 일자, rownum) - sum(출고) OVER(PARTITION BY 상품코드 ORDER BY 일자, rownum) 현재재고--일자별순번컬럼 없어서 rownum으로 대체 , 입고 , 출고 from t1