거래처 원장을 만들어 만들려 하는데 도저히 어떻게 하여야 할지 몰라서 이렇게 질문을 드립니다.
tbl_inout(입출금 테이블)
id acc_id cust_code inout_day dr_amt cr_amt
-- -------- ------------- ------------- ----------- ------------
1 108 0001 20140125 10000 0
2 108 0001 20140130 5000
3 108 0001 20140225 5000
tbl_sumtot(요약테이블)
acc_id cust_code inout_day dr_amt cr_amt
-------- ------------- ------------- ---------- -----------
108 0001 20140000 2000 (전년도 마감액)
108 0001 20140125 10000
108 0001 20140130 5000
108 0001 20140225 5000
출력형식 (기간 20140101~20140228 / 계정과목: 108 / 거래처코드 0001)
id inout_day dr_amt cr_amt jan_amt
--- -------------- ------------ ------------ --------------
전기이월 2000 2,000
1 20140125 10000
2 20140130 5000
3 20140225 5000 12,000
이런 형식으로 하고자 하는데 어떤식으로 응용을 하여자 하는지 잘모르겠습니다.
Comment 7
-
건우아빠
2014.08.29 10:37
with tbl_sumtot as(select '108' acc_id ,'0001' cust_code ,'20140000' inout_day ,2000 dr_amt ,0 cr_amt union allselect '108' ,'0001' ,'20140125' ,10000 , 0 union allselect '108' ,'0001' ,'20140130' ,0 ,5000 union allselect '108' ,'0001' ,'20140225' , 5000 ,0) ,res as (select cust_code , inout_day, dr_amt ,cr_amtfrom tbl_sumtotwhere acc_id = '108' and cust_code = '0001' )select a.cust_code, row_number() over(order by a.inout_day ) - 1 id, case when right(a.inout_day,4) = '0000' then '전기이월' else a.inout_day end [inout_day], a.dr_amt, a.cr_amt, sum( b.dr_amt - b.cr_amt) jan_amtfrom res a left join res b on a.inout_day >= b.inout_daygroup by a.cust_code , a.inout_day , a.dr_amt ,a.cr_amt -
슈토파이터
2014.08.29 14:55
아..이런 방법이 있었내요..union ....ㅎㅎ
감사 합니다...활용 해보겠습니다
-
슈토파이터
2014.08.29 15:28
이 구문과 함수가 ...이버전과 안맞는다네요,..제것은 2000 버전이라서
-
건우아빠
2014.08.29 16:56
with 구문을 인라인 뷰로 하시면 되고
tbl_sumtot은 기존 테이블을 보여 준거라 그대로 사용하시면 되고
res 부분은 임시 테이블로 저장해서 사용하시거나 인라인뷰로 하시면 됩니다.
select a.cust_code
, sum(1) - 1 id
, case when right(a.inout_day,4) = '0000' then '전기이월' else a.inout_day end [inout_day]
, a.dr_amt
, a.cr_amt
, sum( b.dr_amt - b.cr_amt) jan_amt
from (select cust_code , inout_day, dr_amt ,cr_amt
from tbl_sumtot
where acc_id = '108' and cust_code = '0001' ) a
left join
(select cust_code , inout_day, dr_amt ,cr_amt
from tbl_sumtot
where acc_id = '108' and cust_code = '0001' ) b on a.inout_day >= b.inout_day
group by a.cust_code , a.inout_day , a.dr_amt ,a.cr_amt
-
건우아빠
2014.08.29 16:59
create table #res
(
cust_code varchar(10),
inout_day varchar(10),
dr_amt decimal(15,0),
cr_amt decimal(15,0)
)
insert into #res
select cust_code , inout_day, dr_amt ,cr_amt
from tbl_sumtot
where acc_id = '108' and cust_code = '0001'
select a.cust_code
, sum(1) - 1 id
, case when right(a.inout_day,4) = '0000' then '전기이월' else a.inout_day end [inout_day]
, a.dr_amt
, a.cr_amt
, sum( b.dr_amt - b.cr_amt) jan_amt
from #res a left join #res b on a.inout_day >= b.inout_day
group by a.cust_code , a.inout_day , a.dr_amt ,a.cr_amt
-
건우아빠
2014.08.29 17:04
저도 깜박.. 2000용이라고 했는데..
with , row_number()는 2005이상...
디비 업글을 추천합니다...
-
슈토파이터
2014.08.29 17:33
감사~~감사~~~합니다.