웹프로그래밍

Global It Leader!!


MySql


 
 

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

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 5,778회 작성일 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 목록
번호 제목 글쓴이 조회 날짜
56 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 9853 05-07
55 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7499 07-27
54 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5940 07-28
열람중 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5779 07-29
52 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5656 07-18
51 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5598 07-18
50 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5570 07-18
49 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5513 07-28
48 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5161 07-18
47 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5095 08-03
46 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5047 02-28
45 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4749 07-30
44 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4709 01-05
43 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4653 08-16
42 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4625 02-29
41 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4569 02-27
40 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4566 02-21
39 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4440 02-29
38 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4428 03-05
37 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4422 02-27