oracle SQL上的条件SUM [英] Conditional SUM on oracle SQL
本文介绍了oracle SQL上的条件SUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过以下方式获取数据:
I have the data in the follow way:
ITEM LOCATION UNIT RETAIL QUANTITY
100 KS 10 -10
200 KS 20 30
我想要正数量的总和(数量> 0)和负数量的总和(数量< 0).
I want the sum of positive quantities (quantity > 0) and sum of negative quantities (quantity < 0).
如何根据条件获取那些列的总和?
How do I get those column sum based on condition?
推荐答案
您可以使用SUM(CASE ... )
:
SELECT item, location,
SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS positive_sum,
SUM(CASE WHEN quantity < 0 THEN quantity ELSE 0 END) AS negative_sum
FROM your_table
GROUP BY item, location;
LiveDemo
这篇关于oracle SQL上的条件SUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文