I am working in Oracle APEX and I want to update column in a report with a new value in the following table. The Report Query is shown in the following diagram mentioned below.
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;
Required Scenario: I want to update the TotalAmount
Column with Debit
and Credit
. The new Debit
or Credit
value has to be shown in the TotalBalance
column on the next record.
A Brief Introduction to the UPDATE Query in SQL If we want to update a particular value, we use the WHERE clause along with the UPDATE clause. If you do not use the WHERE clause, all the rows will be affected. Moreover, we can use the UPDATE statement to update single or several columns depending on our needs.
You can use Oracle analyitic function 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;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With