CREATE TABLE #your_table(item VARCHAR(10), location VARCH...

0

Please login or register to vote for this query.

(click on this box to dismiss)

MathOverflow

Q&A for professional mathematicians

CREATE TABLE #your_table(item VARCHAR(10), location VARCHAR(10), quantity INT);

INSERT INTO #your_table VALUES ('a', 'a', 1);

INSERT INTO #your_table VALUES ('a', 'a', 10);

INSERT INTO #your_table VALUES ('a', 'a', -45);

INSERT INTO #your_table VALUES ('a', 'a', -3);

INSERT INTO #your_table VALUES ('a', 'a', 12);

INSERT INTO #your_table VALUES ('a', 'a', 2);

INSERT INTO #your_table VALUES ('a', 'b',10);

INSERT INTO #your_table VALUES ('a', 'b',-11);

SELECT 
   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

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results