子查询分解(9i +):
WITH count_cars AS ( SELECt t.person_idCOUNT(*) num_cars FROM CARS c GROUP BY t.person_id), count_children AS ( SELECt t.person_idCOUNT(*) num_children FROM CHILDREN c GROUP BY t.person_id), count_pets AS ( SELECt p.person_idCOUNT(*) num_pets FROM PETS p GROUP BY p.person_id) SELECt t.name, NVL(cars.num_cars, 0) 'Count(cars)', NVL(children.num_children, 0) 'Count(children)', NVL(pets.num_pets, 0) 'Count(pets)' FROM PERSONS tLEFT JOIN count_cars cars ON cars.person_id = t.person_idLEFT JOIN count_children children ON children.person_id = t.person_idLEFT JOIN count_pets pets ON pets.person_id = t.person_id
使用内联视图:
SELECt t.name, NVL(cars.num_cars, 0) 'Count(cars)', NVL(children.num_children, 0) 'Count(children)', NVL(pets.num_pets, 0) 'Count(pets)' FROM PERSONS tLEFT JOIN (SELECt t.person_id COUNT(*) num_cars FROM CARS c GROUP BY t.person_id) cars ON cars.person_id = t.person_idLEFT JOIN (SELECt t.person_id COUNT(*) num_children FROM CHILDREN c GROUP BY t.person_id) children ON children.person_id = t.person_idLEFT JOIN (SELECt p.person_id COUNT(*) num_pets FROM PETS p GROUP BY p.person_id) pets ON pets.person_id = t.person_id



