I am summing the values in one column but I only want the query to show me the summed values if it is less than zero. How would I do this?
我在一列中对值进行求和,但我只希望查询显示总和值,如果它小于零。我该怎么办?
0
lets say you have a table name your_table which have two columns
假设你有一个表名为your_table,它有两列
name balance A -1 B 10 A -5
Now According to your description output will be
现在根据你的描述输出即可
name balance A -6
For this output query will be
对于此输出查询将是
select name,sum(balance) from your_table group by name having sum(balance)<0
You could use a having condition:
having
你可以使用有条件:
SELECT grouping_col, SUM(num_col) FROM mytable GROUP BY grouping_col HAVING SUM(num_col) <0
Try with case when
试试用的情况
SELECT column1,SUM(case when numcolumn<0 then numcolumn else 0 end) as sumval FROM mytable group by column1