I think this will put your existing logic in a form that can execute without errors:select vt.account_sub ,vt.cusip_id ,sum(case when tc.test='unrealized_gain1_sch3a' then vt.cost_ps when tc.test='unrealized_gain1_sch3a' and vt.cost_ps > 0 then vt.cost_ps end) as unrealized_gainfrom vw_perac_transaction as vtleft outer join tbl_maint_transactions_trans_trans_type_cd tc on tc.transaction_type_cd = vt.transaction_type_cdgroup by vt.account_sub ,vt.cusip_id
However, it looks like the logic in your case statements is funky, this slightly simplified statement should return the same results:select vt.account_sub ,vt.cusip_id ,sum(case when tc.test='unrealized_gain1_sch3a' then vt.cost_ps end) as unrealized_gainfrom vw_perac_transaction as vtleft outer join tbl_maint_transactions_trans_trans_type_cd tc on tc.transaction_type_cd = vt.transaction_type_cdgroup by vt.account_sub ,vt.cusip_id
Be One with the OptimizerTG