How to use case and filter with a group by statement and having an aggregate calculation?
FWIW, I think you will be way better of separating the 2 cases for PO.col3, and combine the results with a UNION (which I assume is what you had before). Meanwhile, I tried to fix your query so that it compiles in your fiddle:

SELECT * FROM (
    SELECT
        PO.col1,
        PO.col2,
        CASE WHEN PO.col3 &lt;&gt; 8 THEN PO.col4::float
             WHEN PO.col3 = 8 
             THEN CASE WHEN (CAST(PO.col4 AS float) - SUM(CAST(PO.col5 AS float))) &gt; 0 
                        AND SUM(CAST(PO.col5 AS float)) &gt; 0
                       THEN (CAST(PO.col4 AS float) - SUM(CAST(PO.col5 AS float)))
                       END
             END AS Quantity
    FROM mytable AS PO
    GROUP BY PO.col1, PO.col2, PO.col3, PO.col4 
) AS T
WHERE Quantity &gt; 0


I have no idea whether it is correct or not, for that I would need a small sample and an expected result.