입고테이블, 출고테이블을 이용한 재고 계산 쿼리
페이지 정보
작성자 오원장쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 5,846회 작성일 11-07-29 22:31본문
먼제 인덱스를 추가하고....
ALTER TABLE `db명`.`입고테이블명` ADD INDEX `in_mix1` ( `in_code` , `in_pumno` );
ALTER TABLE `db명`.`출고테이블명` ADD INDEX `pr_mix1` ( `pr_pumno` , `pr_code` );
1. 쿼리 1번
select in_code, max(in_pumno), max(in_dcno), sum(in_amt), sum(pr_amt), sum(in_amt) - sum(pr_amt), max(in_vendor) from (
SELECT in_code, in_pumno, in_dcno, SUM( in_amount ) as in_amt, in_vendor, 0 as pr_amt
FROM in_table
WHERE in_pumno = '2005173-2'
GROUP BY in_code
union all
SELECT pr_code as in_code, '' as in_pumno, '' as in_dcno, 0 as in_amt,'' as in_vendor, IFNULL( SUM( pr_amount ) , 0 ) AS pr_amt
FROM pr_table
WHERE pr_pumno = '2005173-2'
GROUP BY pr_code
) a
group by in_code
order by in_code
LIMIT 30;
2. 쿼리 2번
pr_table 이 기준이라서 in_table에만 있는건 뺀 쿼리입니다.
select in_code, max(in_pumno), max(in_dcno), sum(in_amt), sum(pr_amt), sum(in_amt) - sum(pr_amt), max(in_vendor) from (
SELECT in_code, in_pumno, in_dcno, SUM( in_amount ) as in_amt, in_vendor, 0 as pr_amt
FROM in_table
WHERE in_pumno = '2005173-2'
GROUP BY in_code
union all
SELECT pr_code as in_code, '' as in_pumno, '' as in_dcno, 0 as in_amt,'' as in_vendor, IFNULL( SUM( pr_amount ) , 0 ) AS pr_amt
FROM pr_table
WHERE pr_pumno = '2005173-2'
and pr_code in(select distinct(in_code) from in_table)
GROUP BY pr_code
) a
group by in_code
order by in_code
LIMIT 30;
3. 쿼리 3
select
'2005173-2' as in_pumno,
in_code,
max(in_dcno),
sum(in_amt),
sum(pr_amt),
sum(in_amt) - sum(pr_amt) as stock,
max(in_vendor)
from (
SELECT
in_code,
in_dcno,
SUM(in_amount) as in_amt,
in_vendor,
0 as pr_amt
FROM in_table
WHERE in_pumno = '2005173-2'
GROUP BY in_code
union all
SELECT
pr_code as in_code,
'' as in_dcno,
0 as in_amt,
'' as in_vendor,
IFNULL( SUM( pr_amount ) , 0 ) AS pr_amt
FROM pr_table
WHERE pr_pumno = '2005173-2'
and pr_code in(select distinct(in_code) from in_table)
GROUP BY pr_code
) T
group by in_code
order by in_code
select in_code, max(in_pumno), max(in_dcno), sum(in_amt), sum(pr_amt), sum(in_amt) - sum(pr_amt), max(in_vendor) from (
SELECT in_code, in_pumno, in_dcno, SUM( in_amount ) as in_amt, in_vendor, 0 as pr_amt
FROM in_table
WHERE in_pumno = '2005173-2'
GROUP BY in_code
union all
SELECT pr_code as in_code, '' as in_pumno, '' as in_dcno, 0 as in_amt,'' as in_vendor, IFNULL( SUM( pr_amount ) , 0 ) AS pr_amt
FROM pr_table
WHERE pr_pumno = '2005173-2'
and pr_code in(select distinct(in_code) from in_table)
GROUP BY pr_code
) a
group by in_code
order by in_code
LIMIT 30;
3. 쿼리 3
select
'2005173-2' as in_pumno,
in_code,
max(in_dcno),
sum(in_amt),
sum(pr_amt),
sum(in_amt) - sum(pr_amt) as stock,
max(in_vendor)
from (
SELECT
in_code,
in_dcno,
SUM(in_amount) as in_amt,
in_vendor,
0 as pr_amt
FROM in_table
WHERE in_pumno = '2005173-2'
GROUP BY in_code
union all
SELECT
pr_code as in_code,
'' as in_dcno,
0 as in_amt,
'' as in_vendor,
IFNULL( SUM( pr_amount ) , 0 ) AS pr_amt
FROM pr_table
WHERE pr_pumno = '2005173-2'
and pr_code in(select distinct(in_code) from in_table)
GROUP BY pr_code
) T
group by in_code
order by in_code
질의를 통해 sir.co.kr 회원이신 명랑폐인님이 작성한 쿼리입니다.
댓글목록
등록된 댓글이 없습니다.