私は、異なる「グループ化」に従って値を合計/計算しようとしていますが、単一のクエリでそれを行う方法を理解することはできません。複数の「グループ」を1つのクエリに結合しますか?
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);
私は部分的に私が探しているものを取得する方法を考え出した:上記に加えて
--(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;
、私は差B_SUMの計算たい - a_sumを、そうクエリの結果は次のようになります。
name col1_sum col2_sum difference
---- -------- -------- ----------
A 25 115 90
B 1 (empty) -1
C 33 1 -32
(empty) 100 43 -57
上記の出力を得るにはどのような方法がありますか?ありがとう!