我只想查询数据,当添加Amount列时,结果将为0,并且具有相同的ColumnA和ColumnB。但问题是当我在Group By中添加ColumnB时,我得到了错误的结果。它还返回其总和不为0的数据。
基本上:
Input:
ColumnA, ColumnB, Amount
123, 555, 10
234, 555, -25
234, 555, 10
234, 555, 15
123, 555, 20
基本上我想要的结果是:
ColumnA, ColumnB, Amount
234, 555, -25
234, 555, 10
234, 555, 15
以下是我找到的代码:
SELECT * FROM table
WHERE ColumnA In
(
SELECT ColumnA FROM table
GROUP BY ColumnA, ColumnB
HAVING SUM(Amount)
)
对此有任何帮助吗?
答案 0 :(得分:0)
这样的事情应该有效
select columnA
, columnB
, amount
from table join
(select columnA A
, columnB B
, sum(amount ) theSum
group by columnA, columnB ) temp on columnA = A
and columnB = B
where theSum <> 0
答案 1 :(得分:0)
SELECT *
FROM t_column t1 -- your table name
WHERE EXISTS (SELECT 1
FROM t_column t2 -- your table name
WHERE t1.columnA = t2.columnA
AND t1.columnB = t2.columnB
HAVING SUM(amount) = 0
)
结果
columnA columnB amount
234 555 -25
234 555 10
234 555 15