在oracle 10g报告中计算错误

时间:2016-08-30 08:34:54

标签: sql oracle plsql

当我们减去这些公式列时,我们在oracle 10g报告构建器中创建了两个公式列,但答案是错误的

1:第一个公式栏

function sale_formulaFormula return Number is

begin
DECLARE
        BALANCE NUMBER;
        n number;
        BEGIN
   SELECT NVL(SUM(PLD.DR),0)-NVL(SUM(PLD.CR),0)
   INTO BALANCE
   FROM 
   LEDGER PLD
  WHERE 
  (PLD.d_ID in (9)) AND PLD.LD_DATE BETWEEN :P_FROM_DATE AND :P_TO_DATE;
        return balance;
        end;
end;

2:第二个公式栏

    function expence_formulaFormula return Number is
begin
  DECLARE
        BALANCE NUMBER;
        n number;
        BEGIN
   SELECT NVL(SUM(PLD.DR),0)-NVL(SUM(PLD.CR),0)
   INTO BALANCE
   FROM 
   LEDGER PLD
  WHERE 
  (PLD.d_ID in (10,19,20,5,18)) AND PLD.LD_DATE BETWEEN :P_FROM_DATE AND :P_TO_DATE;
        return balance;
        end;
end;

当我在其他公式列中减去这些公式列但是答案不是真

3:

function CF_4Formula return Char is
begin
  return nvl(:sale_formula,0)-nvl(:expence_formula,0);
end;

1 个答案:

答案 0 :(得分:0)

从你的代码开始,我会说函数创建本身是错误的,它不会被编译。

其次,我按照你的步骤检查我得到的结果。

create table LEDGER(d_ID number,dr number, cr number,LD_DATE date);
--------------------------------------------------------------

Insert into LEDGER   (D_ID, DR, CR, LD_DATE)
 Values   (9, 12, 43, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER   (D_ID, DR, CR, LD_DATE)
 Values  (9, 100, 200, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER    (D_ID, DR, CR, LD_DATE)
 Values  (18, 899, 786, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER (D_ID, DR, CR, LD_DATE)
 Values (19, 665, 77, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER   (D_ID, DR, CR, LD_DATE)
 Values  (9, 354, 35, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER  (D_ID, DR, CR, LD_DATE)
 Values (5, 43.43, 89.4, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER (D_ID, DR, CR, LD_DATE)
 Values  (10, 77, 654, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER (D_ID, DR, CR, LD_DATE)
 Values  (9, 75, 97, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into LEDGER  (D_ID, DR, CR, LD_DATE)
 Values  (9, 59.45, 69.3, TO_DATE('08/30/2016 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
COMMIT;

--------------------------------------------------------

--1: first formula column

create or replace function sale_formulaFormula 
return Number is
BALANCE NUMBER;
n number;
BEGIN

   SELECT NVL(SUM(PLD.DR),0)- NVL(SUM(PLD.CR),0)
     INTO BALANCE
   FROM LEDGER PLD
   WHERE (PLD.d_ID in (9)) ;
   --Commenting since am taking only 1 day data
   --AND PLD.LD_DATE BETWEEN :P_FROM_DATE AND :P_TO_DATE; 

   Return balance;

end;

--Call
select sale_formulaFormula from dual; 

--Got result -- 156.15

--------------------------------------------------------------------------------

--2: second formula column
create or replace function expence_formulaFormula 
return Number is
BALANCE NUMBER;
n number;
BEGIN

    SELECT NVL(SUM(PLD.DR),0)-NVL(SUM(PLD.CR),0)
    INTO BALANCE
    FROM  LEDGER PLD
    WHERE (PLD.d_ID in (10,19,20,5,18));
    --Commenting since am taking only 1 day data 
    --AND PLD.LD_DATE BETWEEN :P_FROM_DATE AND :P_TO_DATE;

  Return balance;

end;

------------------------------------------------   
--Call
select expence_formulaFormula from dual;

--Got result  -- 78.03
----------------------------------------------------

--3 Function 3
create or replace function CF_4Formula 
return Char is
begin
  --return type is char so converting the result to char 
  return to_char(nvl(sale_formulaFormula,0) - nvl(expence_formulaFormula,0));
end;


--Call
select CF_4Formula from dual;
  

得到了结果 - > 156.15-78.03 = 78.12< - >这是正确的.. !!!!