It would be easier if your posted sample data matches up with your desired results. Even easier if you provided executable sample code. But - guessing - how about this:;with Table1 (RefNum, Val1)as( select 1272989, 14397.38 union all select 1612428, 7709.51 union all select 1793655, 7700.00 union all select 1818997, 7983.81), Table3 (RefNum, TranAmt, TranFlag)as( select 1272989, 100.00, 'F' union all select 1272989, 100.00, 'A' union all select 1272989, 100.00, 'F' union all select 1272989, 100.00, 'B'), LookupTable (TranFlag, Ok2Use)as( select 'F', 1 union all select 'A', 0 union all select 'B', 0)select t1.RefNum ,t1.Val1 ,sum(TranAmt) as TotalTranType1from table1 t1left outer join table3 t3 on t3.RefNum = t1.refNumleft join LookupTable l on l.TranFlag = t3.TranFlagwhere OK2Use is null or Ok2Use = 1group by t1.RefNum ,t1.Val1OUTPUT:RefNum Val1 TotalTranType1----------- --------------------------------------- -----------------1793655 7700.00 NULL1612428 7709.51 NULL1818997 7983.81 NULL1272989 14397.38 200.00
Be One with the OptimizerTG