How to implement decreasing balance using MySQL
P粉473363527
2023-09-01 21:11:53
<p>How to create a declining balance query using mysql to calculate depreciation in accounting</p>
<p>For example, the equipment costs $16,000, has an expected useful life of 5 years, and a depreciation rate of 40%. Depreciation</p>
<pre class="brush:php;toolbar:false;">Year Starting book value Depreciation rate Depreciation amount Ending book value
1 16,000 40% 6,400 9,600
2 9,600 40% 3,840 5,760
3 5,760 40% 2,304 3,456
4 3,456 40% 1,382.40 2,073.60
5 2,073.60 40% 829.44 1,244.16</pre>
<p>How can I create a function/query to obtain the above results? Thank you</p>
You can use recursive CTE to get the results you want. For example:
result:
View running examples on DB Fiddle.
Note: All three parameters are defined in the second line. If you want to change the starting value, interest rate, or number of years, make your changes there.