【发布时间】:2021-11-18 06:48:44
【问题描述】:
我的表格中有如下数据cryptotransactionledger
| id | transaction_typeid | transaction_type | amount | totalcoins |
|---|---|---|---|---|
| 1 | 1 | bitcoin-credit | 30 | 30 |
| 2 | 2 | ethereum-credit | 20 | 50 |
如果我花费比特币,我会在同一张表中添加一个如下所示的新条目,其中 transaction_typeid 为 3,对于 ethereum,transaction_typeid 为 4 类似
| id | transaction_typeid | transaction_type | amount | totalcoins |
|---|---|---|---|---|
| 1 | 1 | bitcoin-credit | 30 | 30 |
| 2 | 2 | etherium-credit | 20 | 50 |
| 3 | 3 | bitcoin-debit | -10 | 40 |
| 4 | 4 | etherium-debit | -5 | 35 |
假设如果我在表格中的最终数据如下所示,我将拥有 35 个比特币和 20 个以太坊。
| id | transaction_typeid | transaction_type | amount | totalcoins |
|---|---|---|---|---|
| 1 | 1 | bitcoin-credit | 30 | 30 |
| 2 | 2 | etherium-credit | 20 | 50 |
| 3 | 3 | bitcoin-debit | -10 | 40 |
| 4 | 4 | etherium-debit | -5 | 35 |
| 5 | 1 | bitcoin-credit | 15 | 50 |
| 6 | 2 | etherium-credit | 10 | 60 |
| 7 | 4 | etherium-debit | -5 | 55 |
从各个贷方中减少所有借方后,如何使用 SQL 得出以下余额摘要
| id | transaction_type | amount | totalcoins |
|---|---|---|---|
| 1 | bitcoin-credit | 35 | 35 |
| 2 | etherium-credit | 20 | 55 |
【问题讨论】:
-
比特币信用 35 作为总硬币的计算方法是什么?
-
@RahulBiswas totalcoins 只不过是每行中的总金额字段。由于比特币信用是第一行,它显示的金额字段的值相同,即 35。对于第二行,它确实金额 + 先前的总币,即 20 + 35 = 55。
-
@Serg 我已经更新了有问题的表结构以包括 id 列。