mysql - MySQL Fill in the Remaining Balance
问题描述
I am attempting to calculate the remaining balance and store it in a record in my table. It's complex though since the place where I need to store it is in the table itself. Here is an explanation:
Accounts_Table
╔═════════╦═══════╗
║ Account ║ Total ║
╠═════════╬═══════╣
║ A ║ $1000 ║
╠═════════╬═══════╣
║ B ║ $800 ║
╠═════════╬═══════╣
║ C ║ $1200 ║
╚═════════╩═══════╝
Detail_Table
╔═════════╦══════╦══════╦═════════╗
║ Account ║ Line ║ Type ║ Amount ║
╠═════════╬══════╬══════╬═════════╣
║ A ║ 001 ║ E ║ $200.00 ║
╠═════════╬══════╬══════╬═════════╣
║ A ║ 002 ║ E ║ $300.00 ║
╠═════════╬══════╬══════╬═════════╣
║ A ║ 003 ║ E ║ $100.00 ║
╠═════════╬══════╬══════╬═════════╣
║ A ║ 004 ║ R ║ ║
╠═════════╬══════╬══════╬═════════╣
║ B ║ 001 ║ E ║ $300.00 ║
╠═════════╬══════╬══════╬═════════╣
║ B ║ 002 ║ R ║ ║
╠═════════╬══════╬══════╬═════════╣
║ C ║ 001 ║ R ║ ║
╚═════════╩══════╩══════╩═════════╝
What am I attempting to do is sum all the Amounts that match each Account, find the difference between that sum and the Total for the Account, and then record that Remaining Balance in the Amount column where the Type Column = R.
So, for example, the Type R line for Account A would become populated with $400.00, which is ($1000.00 - ($300.00 + $200.00 + $100.00)).
So my results would look like:
╔═════════╦══════╦══════╦══════════╗
║ Account ║ Line ║ Type ║ Amount ║
╠═════════╬══════╬══════╬══════════╣
║ A ║ 001 ║ E ║ $200.00 ║
╠═════════╬══════╬══════╬══════════╣
║ A ║ 002 ║ E ║ $300.00 ║
╠═════════╬══════╬══════╬══════════╣
║ A ║ 003 ║ E ║ $100.00 ║
╠═════════╬══════╬══════╬══════════╣
║ A ║ 004 ║ R ║ $400.00 ║
╠═════════╬══════╬══════╬══════════╣
║ B ║ 001 ║ E ║ $300.00 ║
╠═════════╬══════╬══════╬══════════╣
║ B ║ 002 ║ R ║ $500.00 ║
╠═════════╬══════╬══════╬══════════╣
║ C ║ 001 ║ R ║ $1200.00 ║
╚═════════╩══════╩══════╩══════════╝
Here's where I am at the moment. This doesn't work and throws an error about updating the same table i'm reading from, not to mention i'm not even sure i'm approaching this problem correctly:
UPDATE Detail_Table
INNER JOIN Accounts_Table
ON Detail_Table.Account = Accounts_Table.Account
SET
Detail_Table.Amount = Accounts_Table.Total - (SELECT SUM(Amount) FROM Detail_Table AS TempTable WHERE TempTable.Account = Accounts_Table.Account)
WHERE
Detail_Table.Type = 'R'
AND
Detail_Table.Amount IS NULL;
Any help is appreciated!
解决方案
进行计算的查询是:
select a.account, ( a.total - coalesce(sum(d.amount), 0) ) as balance
from accounts_table a join
detail_table d
on a.account = d.account and d.type = 'E'
group by a.account, a.total;
有了这个,只需加入:
update detail_table d left join
(select a.account, ( a.total - coalesce(sum(d.amount), 0) ) as balance
from accounts_table a left join
detail_table d
on a.account = d.account and d.type = 'E'
group by a.account, a.total
) b
on d.account = b.account
set d.amount = b.balance
where d.type = 'R';
推荐阅读
- azure - 使用多个电子邮件接收器/Set-AzActionGroup 更新操作组
- vba - 使用 VBA 将 HTML 粘贴到 Word 中
- python - 字典、json(转储、加载)、defaultdict 是否保留了 python 3.7 的顺序?
- c# - 具有外键关系的 C# Linq 查询
- asp.net-mvc - Xamarin 表单 - Android - 仅在 HTTPS 中握手失败
- sql - MVC 实体框架,查询返回 null
- c# - 如何使用 Selenium C# 在警报弹出窗口中单击复选框。该复选框没有任何 xpath。无法检查,因为右键单击已禁用
- npm - npm ERR!:代码 127。gyp ERR!堆栈错误:命令失败
- reactjs - React Table 7 在子组件中嵌套子组件
- python - 熊猫相对于其他一些列将列堆叠到行