I am running into a problem where I am trying to add two SELECT sums from the same table but with different conditions into one single result.
Here is the code:
DROP TABLE Match CASCADE CONSTRAINTS;
CREATE TABLE Match
(
Heim VARCHAR(50),
Gast VARCHAR(50),
HeimP NUMBER,
GastP NUMBER
);
INSERT INTO Match
VALUES ('Bayern München', 'Borussia Dortmund', 1, 1);
INSERT INTO Match
VALUES ('Borussia Dortmund', 'Bayern München', 0, 3);
INSERT INTO Match
VALUES ('Bayern München', 'Schalke', 3, 0);
INSERT INTO Match
VALUES ('Schalke', 'Bayern München', 0, 3);
COMMIT;
SELECT SUM(HeimP) AS Heimpoints
FROM Match
WHERE Heim = 'Bayern München';
SELECT SUM(GastP) AS Gastpoints
FROM Match
WHERE Gast = 'Bayern München';
You can use a
CASE
expression insideSUM()
:See the demo.
Results: