Column that is the difference between values of same column perday

后端 未结 2 1744
孤城傲影
孤城傲影 2021-01-29 06:46

My table is as follows:

Balance          Value 1        Value2          Date        Daily Change (Expected)  
8                8,648,668.60   12,620,390.51   3/2         


        
相关标签:
2条回答
  • 2021-01-29 07:15

    I used the following to answer this question:

    SELECT t1.*,t1.balance-t2.balance Daily_Change FROM TestData t1 LEFT JOIN TestData t2 ON t2.report_date=DATE_SUB(t1.report_date, INTERVAL 1 DAY) AND t1.account=t2.account ORDER BY t1.report_date,t1.account

    0 讨论(0)
  • 2021-01-29 07:16

    You can use a left join:

    select t.*,
           (t.balance - tprev.balance) as daily_change
    from `table` t left join
         `table` tprev
         on t.date = tprev.date  + interval 1 day;
    
    0 讨论(0)
提交回复
热议问题