MySQL具有日期范围的期初期末余额
我的MySQL数据库中有一个表MySQL具有日期范围的期初期末余额,mysql,sql,Mysql,Sql,我的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 ... ... 我的目标是用查询参数fr
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\u date
和to\u date
显示查询结果
从表格打开的开始日期起,将为0,计算将为
期初(+)票据(+)到期(-)期末=(期初+票据)-到期
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
....
....
我想不出对于我的“从日期开始”
,获取上一个日期结束
的最佳方式是什么
从日期=2020-03-03和到日期=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,可能会有数千条记录,因此最佳做法是什么?正式:
SELECT @balance opening, bill, due, @balance := @balance + bill - due closing
FROM source_table, (SELECT @balance := 0) variable
/* ORDER BY trx_date */
;
但所示样本数据中没有唯一的标准-因此结果不是确定性的。正式:
SELECT @balance opening, bill, due, @balance := @balance + bill - due closing
FROM source_table, (SELECT @balance := 0) variable
/* ORDER BY trx_date */
;
但所示样本数据中没有唯一的标准,因此结果不是确定性的。试试看
SET @fromDate='2020-03-03', @toDate='2020-03-04';
SET @minDate = (SELECT MIN(trx_date) FROM transactions);
SET @previousDate=DATE_SUB(@fromDate,INTERVAL 1 DAY);
SELECT trx_date,@balance opening, bill, due, @balance := @balance + bill - due closing
FROM transactions, (SELECT @balance := (SELECT SUM(bill)-SUM(due) FROM transactions WHERE trx_date BETWEEN @minDate AND @previousDate)) variable
WHERE trx_date BETWEEN @fromDate AND @toDate ORDER BY trx_date;
试一试
您需要计算从第一个trx\u日期开始的所有期初
和期末
值,然后根据您感兴趣的日期过滤结果:
SELECT trx_date, opening, bill, due, closing
FROM (
SELECT trx_date,
@balance AS opening,
bill,
due,
@balance := @balance + bill - due AS closing
FROM transactions
CROSS JOIN (SELECT @balance := 0) ini
ORDER BY trx_date
) tx
WHERE trx_date BETWEEN '2020-03-03' AND '2020-03-04'
输出:
trx_date opening bill due closing
2020-03-03 220 200 20 400
2020-03-04 400 110 10 500
您需要计算从第一个trx\u日期开始的所有期初
和期末
值,然后根据您感兴趣的日期过滤结果:
SELECT trx_date, opening, bill, due, closing
FROM (
SELECT trx_date,
@balance AS opening,
bill,
due,
@balance := @balance + bill - due AS closing
FROM transactions
CROSS JOIN (SELECT @balance := 0) ini
ORDER BY trx_date
) tx
WHERE trx_date BETWEEN '2020-03-03' AND '2020-03-04'
输出:
trx_date opening bill due closing
2020-03-03 220 200 20 400
2020-03-04 400 110 10 500
您如何计算期初
和期末
?或者它已经在表中?请提供您已经尝试过的SQL语句。min date=开始0开始(+)账单(+)到期(-)结束=(开始+账单)-due您的MySQL版本是什么?@Akina MySQL版本5.7.24您如何计算开始
和结束
?或者它已经在表中?请提供您已经尝试过的SQL语句。min date=开始0开始(+)账单(+)到期(-)结束=(开始+账单)-due您的MySQL版本是什么?@Akina MySQL版本5.7.24感谢您的回复。通过此查询,对于任何日期范围,期初余额始终以0
开头。请最后读我的问题part@Akashkhan将variable
子查询中的起始余额从0更改为所需的值。感谢您的回复。通过此查询,对于任何日期范围,期初余额始终以0
开头。请最后读我的问题part@Akashkhan将变量
子查询中的起始余额从0更改为所需的值。