2011-09-07 3 views
0

나는 pl/sql과 코딩에 비교적 익숙하고 도움을 주셔서 감사합니다. 다음과 같이 2 개의 테이블을 준비하십시오. 날짜 범위의 합계 (amt1) + 합계 (amt2)를 계산하는 함수 작성.다른 커서의 2 가지 변수의 합 계산 - Pl/sql function

나쁜 결과를 줄 수 있으므로 직접 table1과 table2를 연결할 수 없습니다. 커서를 table1에서 sum (amt1)을 계산하고 table2에서 sum (amt2)을 계산하기 위해 다른 커서를 계산했습니다. 어떻게 내가이 컴파일있어 제 기능입니다

Table 1 

Id Amt Date 
1 342 21-May-02 
2 421 30-Mar-01 
3 598 4-May-11 
2 843 14-Jun-12 
4 457 21-May-09 
1 2346 24-Apr-98 

Table 2 


Id Amt2 Date2 
1 342 21-May-02 
2 421 30-Mar-01 
3 598 4-May-11 
2 843 14-Jun-12 
4 457 21-May-09 
1 2346 24-Apr-98 

이 2 커서에서 합 (amt1) + 합계 (amt2)을 계산합니까하지만 난 그것을 테스트입니다 때, 어떤 값을 반환 나던

CREATE OR REPLACE FUNCTION FZ_HH_BY_DATE_COMMITMENT (pidm   number, 
                start_date date, 
                end_date  date, 
                desg   varchar2) 
    RETURN number 
AS 
    total_commit number(13,2) := 0; 
    total_gifts number(13,2) := 0; 
    total_pledges number(13,2) := 0; 
    sp_pidm  number (9); 

    CURSOR date_commit_gifts IS 
    SELECT SUM(azvglst_amt) FROM acu.azvglst 
    WHERE azvglst_pidm IN (pidm, sp_pidm) 
    AND SUBSTR (azvglst_desg, 0, LENGTH (desg)) = desg 
    AND azvglst_gift_date BETWEEN start_date AND end_date 
    AND azvglst_pledge_no = '0000000' 
    AND (azvglst_pgve_code <> '3P' OR azvglst_pgve_code IS NULL); 


    CURSOR all_date_commit_gifts IS 
    SELECT SUM(azvglst_amt) FROM acu.azvglst 
    WHERE azvglst_pidm IN (pidm, sp_pidm) 
    AND azvglst_gift_date BETWEEN start_date AND end_date 
    AND azvglst_pledge_no = '0000000' 
    AND (azvglst_pgve_code <> '3P' OR azvglst_pgve_code IS NULL); 


    CURSOR date_commit_pledges 
    IS 
     SELECT SUM (agvplst_amt_pledged) 
       FROM agvplst 
     WHERE agvplst_pledge_date BETWEEN start_date AND end_date 
       AND SUBSTR (agvplst_desg, 0, LENGTH (desg)) = desg 
       AND agvplst_pidm IN (pidm, sp_pidm) 
       AND agvplst_psta_code NOT IN ('I','C','U'); 

    CURSOR all_date_commit_pledges 
    IS 
     SELECT SUM (agvplst_amt_pledged) 
       FROM agvplst 
     WHERE agvplst_pledge_date BETWEEN start_date AND end_date 
           AND agvplst_pidm IN (pidm, sp_pidm) 
           AND agvplst_psta_code NOT IN ('I','C','U'); 
BEGIN 
    sp_pidm := TO_NUMBER (fz_split_fields (fz_spouse_info (pidm), 1)); 

    IF desg IS NULL 
    THEN 
     OPEN all_date_commit_gifts; 
     FETCH all_date_commit_gifts INTO total_gifts; 
     CLOSE all_date_commit_gifts; 

     OPEN all_date_commit_pledges; 
     FETCH all_date_commit_pledges INTO total_pledges; 
     CLOSE all_date_commit_pledges; 

     ELSE OPEN date_commit_gifts; 
     FETCH date_commit_gifts INTO total_gifts; 
     CLOSE date_commit_gifts; 

     OPEN date_commit_pledges; 
     FETCH date_commit_pledges INTO total_pledges; 
     CLOSE date_commit_pledges; 
     END IF; 
     total_commit := total_gifts + total_pledges; 
     RETURN total_commit; 

종료;

+0

커서로 함수로 파싱중인 정보가있는 값이 반환되는지 여부를 테스트 했습니까? – Ben

답변

0

두 커서를 필요로하지 않고 당신이 할 수 있습니다 :

select id, sum(amt) from 
(select id, amt1 as amt from table1 
    union all 
    select id, amt2 as amt from table2 
) 
group by id 
+0

답변 해 주셔서 감사합니다. 하지만 다른보고 도구에서 사용할 함수로 필요합니다. – Cindy

+0

NVL을 사용할 때 매우 고마워요. 이전에 알고 있었어야합니다. 나는 다른 접근법을 시도 할 것이고 그것이 잘 작동하는지 보게 될 것이다 !! – Cindy

1

업데이트 : 그런데

CREATE OR REPLACE FUNCTION CALC_BY_DATE(
    start_date DATE, 
    end_date DATE) 
    RETURN NUMBER 
IS 
    sum_amt NUMBER; 
BEGIN 
    SELECT SUM(AMT) 
    INTO sum_amt 
    FROM (
     SELECT AMT 
     FROM TABLE1 
     WHERE DATE >= start_date 
     AND DATE < end_date + 1 
     UNION ALL 
     SELECT AMT 
     FROM TABLE2 
     WHERE DATE >= start_date 
     AND DATE < end_date + 1 
     ); 

    RETURN(NVL(sum_amt, 0)); 
END; 

, 그냥 가져 ... 대신 열린 커서의로 선택 사용하는 경우는 괜찮습니다, 닫기.

시간이 오래 되었기 때문에 쿼리 성능과 관련하여 차이가있었습니다.

total_commit := total_gifts + total_pledges 

은 사용한다 :

total_commit := NVL(total_gifts, 0) + NVL(total_pledges, 0) 

당신이에 allways를 NULL NULL을 얻기 위해 무엇이든 요약하면 기억

한 가지 더

대부분의 경우 문제는 라인입니다.

감사합니다.

+0

답변 해 주셔서 감사합니다. 하지만 다른보고 도구에서 사용하는 함수로 사용해야합니다. – Cindy

+2

@Cindy -이 방법으로 모든 커서를 바꿀 수 있습니다. 기본적으로,'sum (amt) from total_commit into (...') 함수는 당신의 함수에서 작동 할 것입니다 .. – APC

+0

Miguel에게 감사드립니다. NVL을 사용할 때 작동했습니다 ... 이전에 그것을 인식 했어야했습니다. 접근하고 잘 작동하는지 확인하십시오! – Cindy