![](/img/trans.png)
[英]Sum a column from a table and subtract it to a summed column in another table
[英]Is there a way in SQL to sum the contents of a column, where another column = something and subtract to this number, another operation?
现在我有这个:
query1 = "SELECT SUM(column1) FROM table WHERE column2 = 'EUR'"
query2 = "SELECT SUM(column3) FROM table WHERE column4 = 'EUR'"
result = query1 - query2
我想将它合并为一个查询,因此它看起来像这样:
query = "SELECT SUM(column1) FROM table WHERE column2 = 'EUR' -
SELECT SUM(column3) FROM table WHERE column4 = 'EUR' "
未经测试,但这应该可以工作
select
Sum(case when column2='EUR' then column1 end) -
Sum(case when column4='EUR' then column3 end)
from table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.