I'm trying to sum / calculate values according to different "group by", but am unable to figure out how to do it in a single query.
CREATE TABLE test(
col1 text,
col2 text,
col3 int
);
INSERT INTO test VALUES('A', '', 10);
INSERT INTO test VALUES('A', 'A', 15);
INSERT INTO test VALUES( '', 'A', 100);
INSERT INTO test VALUES('B', 'C', 1);
INSERT INTO test VALUES('C', '', 33);
I've figured out how to partially get what I'm looking for:
--(this might not be the "correct" way, just my experiments)
SELECT col1 AS name, sum(col3) as col1_sum FROM test GROUP BY col1;
SELECT col2 AS name, sum(col3) as col2_sum FROM test GROUP BY col2;
In addition to the above, I would like a calculation of the difference b_sum - a_sum, so the complete query result would be:
name col1_sum col2_sum difference
---- -------- -------- ----------
A 25 115 90
B 1 (empty) -1
C 33 1 -32
(empty) 100 43 -57
Any ideas how to get the output described above..? Thanks!