简体   繁体   中英

Adding a previous row value with current row

I want to add a new column that calculate the daily gross amount call GrossAmount with some calculation. DailyTransaction(day1) + cash_in(day2) - DailyExpense(day2) = GrossAmount(day2) But i have no idea on how to do it, i have tried several way but its doesn't work.

ALTER TABLE TESTING ADD COLUMN GrossAmount DECIMAL AS 
  (SELECT t1.DailyTransaction + t2.DailyTransaction 
   FROM TESTING t1 
   INNER JOIN TESTING t2 
   WHERE t1.LOANID = t2.LOANID - 1)

在此处输入图片说明

Try the below perhaps. Not sure what you are trying to do with the ALTER TABLE . Is this meant to be a calculated column?

select DailyTransaction + lag(DailyTransaction, 1, 0) 
        over (order by LOANID), DailyTransaction, LOANID
  from TESTING
 order by LOANID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM