웹프로그래밍

Global It Leader!!


MySql


 
 

입고테이블, 출고테이블을 이용한 재고 계산 쿼리

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 5,773회 작성일 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

질의를 통해 sir.co.kr 회원이신 명랑폐인님이 작성한 쿼리입니다.

댓글목록

등록된 댓글이 없습니다.

전체 56
게시물 검색
MySql 목록
번호 제목 글쓴이 조회 날짜
16 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4438 02-29
15 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4622 02-29
14 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5044 02-28
13 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4057 02-28
12 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4420 02-27
11 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4563 02-27
10 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4562 02-21
9 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4708 01-05
열람중 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5774 07-29
7 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5510 07-28
6 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5935 07-28
5 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7496 07-27
4 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5568 07-18
3 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5595 07-18
2 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5158 07-18
1 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5655 07-18