SELECT tablea.id1, tablea.id2, COUNT(*) FROM ... ... GROUP BY id1, id2