예전에 거래처 원장을 조회하는 폼을 질문 드려 도움을 받아 Query를 작성하였습니다..그러다 다른 방법이 생각나서 다시 질문 드립니다
select 0, '', '', '', '', 0, '' , '', '', '', sum(dr_amt)-sum(cr_amt) as dr_amt, 0
from tbl_suntot as A3
where A3.com_id ='H'
and A3.comp_id ='H01'
and A3.acc_id ='108'
and A3.name_code = '0001'
and A3.inout_day >='20140000'
and A3.inout_day <'20140331' 전기이월 잔고
union all
select n_id, com_id, comp_id, sa_id, inout_day, slip_no, bill_id, n_remark, acc_id, name_code, dr_amt, cr_amt
from tbl_suninout as A3
where A3.com_id ='H'
and A3.comp_id ='H01'
and A3.acc_id ='108'
and A3.name_code = '0001'
and A3.inout_day >='20140101'
and A3.inout_day <='20140131' 1월 내역
union all
select 0, '', '', '', '', 0, '' , '', '', '', sum(dr_amt)as dr_amt,sum(cr_amt) as cr_amt
from tbl_suntot as A3
where A3.com_id ='H'
and A3.comp_id ='H01'
and A3.acc_id ='108'
and A3.name_code = '0001'
and A3.inout_day >='20140101'
and A3.inout_day <='20140131' 1월 월계
union all
select n_id, com_id, comp_id, sa_id, inout_day, slip_no, bill_id, n_remark, acc_id, name_code, dr_amt, cr_amt
from tbl_suninout as A3
where A3.com_id ='H'
and A3.comp_id ='H01'
and A3.acc_id ='108'
and A3.name_code = '0001'
and A3.inout_day >='20140201'
and A3.inout_day <='20140231' 2월 내역
union all
select 0, '', '', '', '', 0, '' , '', '', '', sum(dr_amt)as dr_amt,sum(cr_amt) as cr_amt
from tbl_suntot as A3
where A3.com_id ='H'
and A3.comp_id ='H01'
and A3.acc_id ='108'
and A3.name_code = '0001'
and A3.inout_day >='20140201'
and A3.inout_day <='20140231' 2월 월계
이렇게 12개월의 Query를 작성 했습니다 그런데 문제가 있어서 질문 드립니다. 조건을 2014/0101~2014/12/31 이 아닌
2014/02/01~2014/03/31 로 하면 해당 Query안되는 것 같습니다...이 일자 조회 처리를 어찌 해야 하는지요?
Comment 4
-
군고구마
2014.09.05 11:30
-
슈토파이터
2014.09.05 11:52
창피하지만..몰라서 12개월 치를 작성 했습니다...T.T
-
슈토파이터
2014.09.05 11:54
아..그러고 union all 은 필드의 갯수가 서로 일치 해야 한다 해서
tbl_suntot 의 필드 갯수를 tbl_suninout 갯수와 일치 시키기 위해 했습니다...
-
슈토파이터
2014.09.05 12:16
tbl_suntot 는 일자별 집계가 되는 테이블 입니다(Trigger 적용)
tbl_suninout 는 입출금 테이블 입니다..
전기이월 이나 월 합계는 tbl_suntot 에서 가져오고
입추금 내역은 tbl_suninout 에서 가져옵니다
쿼리를 보면 테이블은 tbl_suntot 한개이고, where조건에 날짜는 inout_day가 맞는데, 왜 12개월치를 해서 날짜를 변수가 아닌 정적으로 삽입하나요...? 혹시 이유가 있으신지요??
select n_id, com_id, comp_id, sa_id, inout_day, slip_no, bill_id, n_remark, acc_id, name_code, dr_amt, cr_amt
'select 0, '', '', '', '', 0, '' , '', '', '', sum(dr_amt)as dr_amt,sum(cr_amt) as cr_amt
이 부분 때문에 그런건가요?
그게 아니라면 합쳐서 날짜데이터를 변수로 받아서 조회하도록 해도 될 듯 합니다... 아닐 경우 답글 주세요~
구조를 모르니...