Using Common table expressions
WITH T(I) AS
(
select Sum(Case when (b.test1 <> b.test2
OR (b.test1 IS NULL AND b.test2 IS NOT NULL)
OR (b.test1 IS NOT NULL AND b.test2 IS NULL)) then 1 else 0 end) as sample from table b
UNION
select((Sum(a.counterid)*1.0/Sum(a.fieldcount))*100)as sample2 from table2 a
}
SELECT AVG(I) FROM T