Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATING Table Column with new Entry

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. enter image description here

like image 499
Usman YousafZai Avatar asked Feb 07 '13 09:02

Usman YousafZai


People also ask

How do you update a specific column in SQL?

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.


2 Answers

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;
like image 133
Alen Oblak Avatar answered Oct 12 '22 22:10

Alen Oblak


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;
like image 40
Florin stands with Ukraine Avatar answered Oct 12 '22 20:10

Florin stands with Ukraine