我有一个表格testing(name,money),在这里我必须返回一个结果,其中所有正数的钱都被存入存款,而负数的钱将被提取。在下面的查询中显示:“子查询只允许返回单个列。”
SELECT name, SUM(money) AS "deposit"
FROM testing
where money > 0 in
(SELECT name, SUM(abs(money)) AS "withdraw"
FROM testing
where money < 0 )
GROUP BY name;
p.s:我尝试了没有“ In”的情况,但也无法正常工作
答案 0 :(得分:2)
SELECT name,
SUM(case when money > 0 then money else 0 end) AS deposit,
SUM(case when money < 0 then money else 0 end) AS withdraw
FROM testing
GROUP BY name;
答案 1 :(得分:-1)
您可以使用以下SQL语句
SELECT name, SUM(money) AS "deposit"
FROM testing
where money > 0
AND money in
(
SELECT SUM(abs(money)) AS "withdraw"
FROM testing
where money < 0
)
GROUP BY name;
让我知道它是否有效! :)