SQL查询-不同的结果

| 我有以下两个表:
People [*ID*, Name] 
Pet [*PetID*, OwnerID, Species, Name]
(OwnerID是ID的外键) 我希望数据库列出每个人以及他们拥有多少种不同物种。例如,如果Bob(ID 1473)拥有一只狗,一只猫和另一只狗,则输出应为:
ID    | No. of Species
----------------------
1473  | 2
我意识到这将需要相关的子查询或外部联接,但是我不确定如何做到这一点。任何帮助,将不胜感激。     
已邀请:
        您可以为此使用
count(distinct ...)
select  People.ID
,       count(distinct Species)
from    People
join    Pet
on      Pet.OwnerID = People.ID
group by
        People.ID
    
        
select people.name, count(distinct pet.species)
from people, pet
where people.id = pet.ownerid
group by people.name
    
        尝试这个
Select ID,[No. of Species] from People 
inner join 
  ( select Count(Species) as [No. of Species],OwnerID from Pet  
   group by OwnerID) d 
on Id = d.OwnerID
    

要回复问题请先登录注册