我有一个包含正数和负数的表。我必须使用子查询找出正负数之和
发布于 2013-12-12 20:45:10
您的问题不是很清楚(没有给出表或列名),也不清楚为什么需要一个子查询(如果可以避免的话,这永远不是一个好主意)。您可以通过使用'case‘语句获取所需的值
下面计算正值和负值的数量
select sum (case when acolumn >= 0 then 1 else 0 end) as positive,
sum (case when acolumn < 0 then 1 else 0 end) as negative
from table下面是正值和负值的数量之和
select sum (case when acolumn >= 0 then acolumn else 0 end) as positive,
sum (case when acolumn < 0 then acolumn else 0 end) as negative
from table发布于 2013-12-12 20:23:12
对于负数的总和:
SELECT SUM(numberColumn) FROM tableFoo WHERE numberColumn < 0对于正数的和:
SELECT SUM(numberColumn) FROM tableFoo WHERE numberColumn >= 0要组合这两个查询(其中QUERY1和QUERY2是前面的两个查询):
SELECT (QUERY1), (QUERY2)发布于 2018-05-14 14:32:49
select sum(case when a>=0 then a else 0 end) as positive,
sum(case when a<0 then a else 0 end) as negative
from ahttps://stackoverflow.com/questions/20543397
复制相似问题