【发布时间】:2020-04-27 05:13:06
【问题描述】:
我的 MySQL 数据库中有一个表 transactions,有 50K 条记录。数据如下所示。
trx_date bill due
2020-03-01 100.00 10.00
2020-03-02 50.00 20.00
2020-03-02 100.00 0.00
2020-03-03 200.00 30.00
2020-03-04 100.00 10.00
...
...
我的目标是显示带有查询参数from_date和to_date的查询结果
从开桌之日起为0,计算为
opening(+) bill (+) due (-) closing = ( opening+bill ) - due
trx_date opening bill due closing
2020-03-01 0 100.00 10.00 90.00
2020-03-02 90.00 50.00 20.00 120.00
2020-03-02 120.0 100.00 0.00 220.00
2020-03-03 220.00 200.00 20.00 400.00
2020-03-04 400.00 110.00 10.00 500.00
....
....
我不知道为我选择的 from_date opening 获取上一个日期 closing 的最佳方式是什么。
from_date=2020-03-03 和 to_date=2020-03-04
trx_date opening bill due closing
2020-03-03 220.00 200.00 20.00 400.00
2020-03-04 400.00 110.00 10.00 500.00
我的意思是如何从之前的结果中打开220.00?从之前的结果中得到 220.00 可能有数千条记录,那么最佳实践是什么?
【问题讨论】:
-
你是如何计算
opening和closing的?还是已经在表格中了? -
请提供您已经尝试过的SQL语句。
-
最小日期 = 开 0 开 (+) 账单 (+) 到期 (-) 关 = ( 开 + 账单 ) - 到期
-
你的 MySQL 版本是多少?
-
@Akina MySQL 版本 5.7.24