I have the data in the follow way:
ITEM LOCATION UNIT RETAIL QUANTITY
100 KS 10 -10
200 KS 20 30
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?
You can use
GREATEST
andLEAST
in conjunction with theSUM
function:SQL Fiddle
Oracle 11g R2 Schema Setup:
Query 1:
Results:
You can use
SUM(CASE ... )
:LiveDemo