SELECT myparent.iid, sum(IIF(ISNULL(T1.pid),0,1) as t1.pid,; sum(IIF(ISNULL(T2.pid),0,1) as t2.pid,; sum(IIF(ISNULL(T3.pid),0,1) as t3.pid,; sum(IIF(ISNULL(T4.pid),0,1) as t4.pid ; FROM myparent LEFT JOIN T1 ON myparent.iid = t1.pid ; LEFT JOIN T2 ON myparent.iid = t2.pid ; LEFT JOIN T3 ON myparent.iid = t3.pid ; LEFT JOIN T4 ON myparent.iid = t4.pid ; GROUP BY Myparent.iidmaybe the sum() construct could be replace by count (distinct Tx.Pk), but i haven't tested this.
>myParent.iid t1.pid t2.pid t3.pid t4.pid >------------ ------ ------ ------ ------ >1 1 2 1 1 >2 1 3 5 2 >3 1 3 5 3 >4 2 4 7 4 >5 4 7 6 >6 4 6 >7 >8 >9 > >* Desired output > >pid Cnt1 Cnt2 Cnt3 Cnt4 >--- ---- ---- ---- ---- > 1 3 1 1 > 2 1 1 1 > 3 2 1 > 4 2 1 1 > 5 2 > 6 2 > 7 2 > 8 > 9Any idea how I could do this just with one SQL ? TIA