Create table #tempa
(
id_A int,
Cnt_A bigint
)
Create table #tempb
(
id_B int,
Cnt_b bigint
)
insert into #tempa
select A.id,count(A.id)
from tableA A join tablec C on A.id=c.id
group by A.id
insert into #tempb
select b.id,count(b.id)
from tableb b join tablec C on b.id=c.id
group by b.id
--Return Table
select A.ID , cnt_a/cnt_b from
#tempa a join #tempb b on a.id_A=b.id_B
Actual tables means tables used in UDF..