sql - mysql obtain running difference -
i have below table structure , data:
----------------------------- | date | accu. output | ----------------------------- | 2017-01-01 | 150 | | 2017-01-02 | 165 | | 2017-01-03 | 182 | | 2017-01-04 | 190 | | 2017-01-05 | 210 | -----------------------------
i need generate below output above dataset. can client side processing. trying write query this.
---------------------------------------------- | date | accu. output | daily output | ---------------------------------------------- | 2017-01-05 | 210 | 20 | | 2017-01-04 | 190 | 8 | | 2017-01-03 | 182 | 17 | | 2017-01-02 | 165 | 15 | | 2017-01-01 | 150 | 0 | ----------------------------------------------
daily output difference between current accu. output , previous day's accu. output.
thanks
here method uses left join:
select t.*, coalesce(t.accu_output - tprev.accu_output, 0) diff t left join t tprev on tprev.date = t.date - interval 1 day;
this assumes -- in sample data -- days increasing no gaps , 1 value per day. these conditions implied question (previous day's output).
Comments
Post a Comment