I have a PostgreSQL table like this:
CREATE TABLE foo(man_id, subgroup, power, grp)
AS VALUES
(1, 'Sub_A', 4, 'Group_A'),
(2, 'Sub_B', -1, 'Group_A'),
(3, 'Sub_A', -1, 'Group_B'),
(4, 'Sub_B', 6, 'Group_B'),
(5, 'Sub_A', 5, 'Group_A'),
(6, 'Sub_B', 1, 'Group_A'),
(7, 'Sub_A', -1, 'Group_B'),
(8, 'Sub_B', 2, 'Group_B'),
(9, 'Sub_C', 2, 'Group_B');
The power calculation works like this:
- Total Power of Subgroup Sub_A in the grp Group_A is (4 + 5 ) = 9
- Total Power of Subgroup Sub_B in the grp Group_A is ((-1) + 1 ) = 0
- Total Power of Subgroup Sub_A in the grp Group_B is ((-1) + (-1) ) = -2
- Total Power of Subgroup Sub_B in the grp Group_B is (6 + 2 ) = 8
So the power of Sub_A in the Group_A is not equal to power of Sub_A in the Group_B
So the power of Sub_B in the Group_A is not equal to power of Sub_B in the Group_B
I want to query the database and fetch the rows where, for a same subgroup
name total power
is not equal across all the other grp
names.
What would be the recommended way to do this?
I can find the sum of total power:
SELECT sum(power) AS total_power
FROM foo
GROUP BY grp
MySQL solution will be accepted as well.