Global It Leader!!



 
 

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

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 6,755회 작성일 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 회원이신 명랑폐인님이 작성한 쿼리입니다.

댓글목록

등록된 댓글이 없습니다.

전체 45
게시물 검색
컴퓨터언어 목록
번호 제목 글쓴이 조회 날짜
25 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4816 08-10
24 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6034 08-03
23 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5157 08-03
22 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5651 07-30
21 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5003 07-27
20 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4973 03-10
19 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6410 03-05
18 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5269 03-05
17 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4978 03-04
16 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5330 02-29
15 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7170 02-29
14 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6044 02-28
13 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4963 02-28
12 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5276 02-27
11 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5806 02-27
10 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5299 02-21
9 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5637 01-05
열람중 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6756 07-29
7 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6027 07-28
6 Mysql no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 6564 07-28