gpt4 book ai didi

mysql - 如何在基于另一列的同一行中减去mysql

转载 作者:可可西里 更新时间:2023-11-01 07:49:49 25 4
gpt4 key购买 nike

我正在尝试用买入的美元减去卖出的美元。

**|Tran_ID|Currency_type|currency_Amount|Local_Amount|  Rate  |Tran_type| ** 
| 85 | USD | 2500 | 1581.54 |1.58074 | Bought |
| 78 | USD | 3487.57 | 2206.29 |1.58074 | Bought |
| 86 | USD | 100 | 63.26 |1.58074 | Bought |
| 87 | USD | 94.17 | 63.26 |1.48866 | Sold |
| 88 | USD | 2600 | 1746.54 |1.48866 | Sold |
| 89 | USD | 2600 | 1746.54 |1.48866 | Sold |

我可以使用下面的查询添加卖出或买入的美元总值

SELECT Currency_type, sum( currency_Amount ) AS total_bought
FROM Transaction
WHERE Currency_type= 'USD'
AND Tran_type = 'Bought' <== **or 'Sold'**

结果如下

**|Currency_type | total_bought    |**
|USD |6087.570068359375|

最佳答案

您可以根据您的交易类型获得一个标志:

SUM(currency_Amount * IF(Tran_type = "Bought", 1, -1))

关于mysql - 如何在基于另一列的同一行中减去mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16059375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com