select a.invNo, a.totalamt/c.num, b.orderno
from A join B on a.invNo = b.invNo
join (select invno, count(1) as num from B group by invno) c on a.invno = c.invno
or
select a.invNo, a.totalamt/count(b.*)
from A join B on a.invNo = b.invNo
group by a.invno, a.totalamt
select a.invno,casr when isnull(b.cnt,0)=0 then a.totalamt else a.totalamt/t.cnt end as totalamt
from a left join (
select invno,count(distinct orderno) as cnt
from b
group by invno
) as t
on a.invno=t.invno