如何创建 MySql 查询以显示 运行 多个客户拥有个人余额的贷方和借方余额

how to create a MySql query to display running balance from credit and debit where multiple customers have indivdual balances

我有一个 table 结构如下: enter image description here

Table是针对不同客户的简单贷记和借记table(每个客户都有自己的ID)

交易发生在不同的日期。每笔交易都有自己的 ID,按时间顺序生成。

必须创建一个视图来显示每个客户的 运行 余额。该视图给出了按时间顺序排列的列表。

列表项是

  1. 交易:

我想获取解决上述问题的查询代码。 感谢您的期待。

这应该有效

  select  transaction_id, customer_id, date, credit, debit,
  abs(sum(ifnull(credit,0)) over (partition by customer_id order by date,credit,transaction_id ) - sum(ifnull(debit,0)) over(partition by customer_id order by date,debit,transaction_id)) as balance
  from ledger
  order by transaction_id;