I have the following table:
CREATE TABLE tablename ("ID" varchar2(1), "Type" varchar2(3), "Value" int);
INSERT ALL
INTO tablename ("ID", "Type", "Value")
VALUES ('A', 'MS',2)
INTO tablename ("ID","Type", "Value")
VALUES ('A', 'MS', 5)
INTO tablename ("ID", "Type", "Value")
VALUES ('A', 'MSH', 6)
INTO tablename ("ID", "Type", "Value")
VALUES ('A', 'MSH', 10)
INTO tablename ("ID", "Type", "Value")
VALUES ('A', 'MSO', -5)
INTO tablename ("ID", "Type", "Value")
VALUES ('A', 'MSO', 12)
INTO tablename ("ID", "Type", "Value")
VALUES ('B', 'MS',5)
INTO tablename ("ID","Type", "Value")
VALUES ('B', 'MS', -4)
INTO tablename ("ID", "Type", "Value")
VALUES ('B', 'MSH', 2)
INTO tablename ("ID", "Type", "Value")
VALUES ('B', 'MSH', 11)
INTO tablename ("ID", "Type", "Value")
VALUES ('B', 'MSO', -5)
INTO tablename ("ID", "Type", "Value")
VALUES ('B', 'MSO', 13)
SELECT * FROM dual
;
The table will be grouped by ID and Type and use the sum of the Values. Now I want to get the difference of MS-MSH and MS-MSO for each ID.
So the result should be something like
ID | Type | sum(value) | Dif
A | MS | 7 | 0
A | MSH | 16 | -9
A | MSO | 7 | 0
B | MS | 1 | 0
B | MH | 13 | -12
B | MSO | 9 | -8
Here is the table to work with