Zvažte použití poddotazu. Přidal jsem aliasy tabulek, aby byl dotaz čitelnější:
select *
, item_unitprice * total_ecuation as item_subtotal
, (item_unitprice * total_ecuation) - item_discount as item_total
from (
select ri.item_code
, case status_code
when 11 then ri.item_quantity - rr.item_quantity
when 12 then ri.item_quantity + rr.item_quantity
end as total_ecuation
, rr.item_unitprice
, rr.item_quantity
, rr.item_discount * rr.item_quantity as item_discount
from qa_returns_residues rr
join qa_returns_items ri
on ri.item_code = rr.item_code
) as SubQueryAlias
where total_ecuation > 0