Skip to content
Advertisement

Sum rows with same Id based on type and exlude where SUM = 0

I have this table MOVEMENTS:

I’m trying to get all the FatherId with the SUM of IN – OUT Movments > 0. So the result would be:

FatherId = B not showing because SUM(MovementType = IN) – SUM (MovementType = OUT) = 0

I tried with

That gives me the result grouped by FatherId, but I’m not able to filter with Total > 0, and also, I’m unable to put this query in a Subquery like:

Is this doable without a stored procedure? Thank you for any help

Advertisement

Answer

Why are you using a subquery? This should do what you want:

You can also simplify the logic to use a single SUM():

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement