insert into cost (Ordernum, ResType, Resource, Terminal, Division,
TotalMiles, LegMiles)
select distinct OrderNum,
case
when lgh_tractor <> 'UNKNOWN' then 'Tractor'
when lgh_carrier is not null then 'Carrier'
end,
case
when lgh_tractor <> 'UNKNOWN' then lgh_tractor
when lgh_carrier is not null then lgh_carrier
end,
lgh_class1,
lgh_class2,
(select sum(lgh_miles) from legheader l2 where l2.ord_hdrnumber = OrderNum),
(select sum(lgh_miles) from legheader l3 where l3.ord_hdrnumber = OrderNum
and l3.lgh_tractor = l.lgh_tractor)
from revenue w left join legheader l on w.OrderNum = l.ord_hdrnumber
select LegMiles/TotalMiles, * from cost
|