gpt4 book ai didi

mysql - 一张表的SQL算术运算

转载 作者:行者123 更新时间:2023-11-29 10:21:00 27 4
gpt4 key购买 nike

您好,我有交易表。 BigDecimal 金额和 bool 结果。如果结果为真,我就买了一些东西,如果结果为假,我就卖掉了一些东西。当我做这样的事情时,我想在单个查询中获取金额

SELECT (amount(when outcome is false) - amount(when outcome is true)) AS income 
FROM transaction WHERE (the condition I was unable to write);

有没有办法在单个查询中做到这一点?

最佳答案

我认为你只想要条件聚合:

SELECT SUM(CASE WHEN outcome THEN - amount ELSE amount END) as income 
FROM transaction;

关于mysql - 一张表的SQL算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253569/

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