使用新条目更新表列

时间:2013-02-07 09:54:39

标签: sql oracle oracle-apex

我正在使用Oracle APEX,我希望使用下表中的新值更新报表中的列。报告查询如下图所示。

TABLE  "VENDOR_ACCOUNT" 
(  "VEN_ACCOUNTID"      NVARCHAR2(10), 
   "VEN_REGNO"          NVARCHAR2(10), 
   "VEN_TXDATE"         DATE, 
   "VEN_INVOICE_REFNO"  NVARCHAR2(10), 
   "TOTALAMOUNT"        NVARCHAR2(10), 
   "IN"                 NUMBER(10,0), 
   "OUT"                NUMBER(10,0) 
) 

SELECT "VEN_ACCOUNTID" , 
       "VEN_REGNO" , 
       "VEN_TXDATE" , 
       "VEN_INVOICE_REFNO" , 
       "TOTALAMOUNT" , 
       "IN",
       "OUT",   
       "TOTALAMOUNT"+"IN" as "CREDIT",
       "TOTALAMOUNT"-"OUT" as "DEBIT"
FROM Vendor_Account;

必填情景:我想使用TotalAmountDebit更新Credit列。新的DebitCredit值必须显示在下一条记录的TotalBalance列中。 enter image description here

2 个答案:

答案 0 :(得分:5)

Select
    "VEN_ACCOUNTID" , 
    "VEN_REGNO" , 
    "VEN_TXDATE" , 
    "VEN_INVOICE_REFNO" , 
    "TOTALAMOUNT" AS "OLD_TOTALAMOUNT"
    "TOTALAMOUNT" + nvl(lag (nvl("IN",0)-nvl("OUT",0)) 
                             over (partition by "VEN_REGNO" order by "VEN_ACCOUNTID" 
                             )
                        ,0) AS "TOTALAMOUNT"
    "IN",
    "OUT",  
 from Vendor_Account;

答案 1 :(得分:5)

您可以使用Oracle分析函数LAG

Select "VEN_ACCOUNTID" , 
        "VEN_REGNO" , 
        "VEN_TXDATE" , 
        "VEN_INVOICE_REFNO" , 
        "TOTALAMOUNT" , 
        "TOTALAMOUNT" + lag("IN",1) over (ORDER BY "VEN_ACCOUNTID")
                      - lag("OUT",1) over (ORDER BY "VEN_ACCOUNTID") AS "NEW_TOTALAMOUNT",
        "IN",
        "OUT",  
        "TOTALAMOUNT"+"IN" as "CREDIT",
        "TOTALAMOUNT"-"OUT" as "DEBIT"
FROM    Vendor_Account;