WITH A AS
(
SELECT '20090901' DT , 10 CNT, '1' CD FROM DUAL UNION ALL
SELECT '20090901' DT , 30 CNT, '2' CD FROM DUAL UNION ALL
SELECT '20090901' DT , 10 CNT, '1' CD FROM DUAL UNION ALL
SELECT '20090902' DT , 10 CNT, '1' CD FROM DUAL
)
SELECT A.*
, CASE WHEN CNT > 0 THEN '들어옴' ELSE '나옴' END TT
FROM ( SELECT DT
, NVL( SUM( CASE WHEN CD = '1' THEN CNT END ),0) - NVL( SUM( CASE WHEN CD = '2' THEN CNT END ),0) CNT
FROM A
GROUP BY DT
) A
아..산식은
(자재재고변동이 있던 날) (해당일까지 전체 재고) 입니다.
20090901 (10 - 30 + 10)
20090902 (10 - 30 + 10 + 10) 이 되겠네요 ^^;;
by 러드
[2009.09.24 15:49:09]
WITH A AS
(
SELECT '20090901' DT , 10 CNT, '1' CD FROM DUAL UNION ALL
SELECT '20090901' DT , 30 CNT, '2' CD FROM DUAL UNION ALL
SELECT '20090901' DT , 10 CNT, '1' CD FROM DUAL UNION ALL
SELECT '20090902' DT , 10 CNT, '1' CD FROM DUAL
)
SELECT DT
, TT
FROM ( SELECT DT
, SUM( CASE WHEN CD = 2 THEN -CNT ELSE CNT END ) OVER( ORDER BY ROWNUM ) TT
, ROW_NUMBER() OVER( PARTITION BY DT ORDER BY ROWNUM DESC ) RN
FROM A
)
WHERE RN = 1
by 임형섭
[2009.09.24 16:38:51]
러드님 감사합니다.
sum에 이런 용법이 있는줄 몰랐네요;;
오늘 크게 배우고 갑니다.
정말 감사드려요 ^^
by 러드
[2009.09.24 16:43:04]
모델로 보고 있는데 역시 감이 안오네요 ㅠㅠ
by 임형섭
[2009.09.24 19:05:23]
모델로 보는게 어떤건가요?
by 마농
[2009.09.25 14:14:41]
WITH t AS
(
SELECT '20090901' dt, 10 cnt, '1' cd FROM dual
UNION ALL SELECT '20090901', 30, '2' FROM dual
UNION ALL SELECT '20090901', 10, '1' FROM dual
UNION ALL SELECT '20090902', 10, '1' FROM dual
)
SELECT dt
, SUM(DECODE(cd,'1',cnt,-cnt)) cnt
, SUM(SUM(DECODE(cd,'1',cnt,-cnt))) OVER(ORDER BY dt) sum_cnt
FROM t
GROUP BY dt
;
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.