繁体   English   中英

SQL 中是否有一种方法可以对一列的内容求和,其中另一列 = 某物并减去该数字,另一个操作?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM