有一个 mysql 查询,我希望使用多个条件总和。当我在同一行中使用 2 wheres 时,它显示错误
There is this mysql query i wish to get use multiple conditioned sum . As i use 2 wheres in the same line it shows an an error
查询是
SELECT ANO((SUM(AMOUNT) WHERE ANO = 101 AND TYPE = DEPOSIT) -
(SUM(AMOUNT) WHERE ANO = 101 AND TYPE = WITHDRAW)) AS TOTAL
FROM transact
WHERE ANO = 101;
我猜 2 Where create problem。
我想获取当前余额。
例如 - 101 有 12000 的存款和 2500 的提款。输出应该是 9500。
我怀疑你想要条件聚合:
select ano,
sum(case when type = 'deposit' then amount else - amount end) as balance
from transact
where type in ('deposit', 'withdraw')
group by ano
如果只有两个可能的 type
,则不需要 where
子句。
查询是
SELECT ANO((SUM(AMOUNT) WHERE ANO = 101 AND TYPE = DEPOSIT) -
(SUM(AMOUNT) WHERE ANO = 101 AND TYPE = WITHDRAW)) AS TOTAL
FROM transact
WHERE ANO = 101;
我猜 2 Where create problem。 我想获取当前余额。 例如 - 101 有 12000 的存款和 2500 的提款。输出应该是 9500。
我怀疑你想要条件聚合:
select ano,
sum(case when type = 'deposit' then amount else - amount end) as balance
from transact
where type in ('deposit', 'withdraw')
group by ano
如果只有两个可能的 type
,则不需要 where
子句。