无法使此查询在SQL(Oracle)中工作

无法使此查询在SQL(Oracle)中工作,sql,oracle,Sql,Oracle,我试图从下面的数据库中返回官员的姓名、他们管理的酒店以及他们裁判的比赛数量,请参见下面的关系图 我尝试了以下方法,但无效: select officials.name as Official, hotels.name as Hotel, -- hotel the official manages count (case when officials.name = matches.referee then 1 else null end) as Matchesrefereed from offic

我试图从下面的数据库中返回官员的姓名、他们管理的酒店以及他们裁判的比赛数量,请参见下面的关系图

我尝试了以下方法,但无效:

select officials.name as Official,
hotels.name as Hotel, -- hotel the official manages
count (case when officials.name = matches.referee then 1 else null end) as Matchesrefereed
from officials
left join hotels
on  officials.staffid = hotels.manager
left join matches
on officials.staffid = matches.referee
where (case when hotels.name is null then '-' else hotels.name end); -- print '-' if does not manage hotel
我得到一个select的group函数错误,最后的case语句也不起作用

参考中的关系图:

你的数据库管理系统是什么

select 
   officials.name as Official,
   nvl(hotels.name, '-') as Hotel, -- hotel the official manages
   count (matches.referee) as Matchesrefereed
  from officials
  left join hotels on  officials.staffid = hotels.manager
  left join matches on officials.staffid = matches.referee
group by officials.name, nvl(hotels.name, '-')

这几乎是正确的,只是它有时会返回一个官员的重复记录,并错误地将其分配给酒店管理人员。@Tanger您能提供一些示例数据吗?一个官员可以管理多家酒店吗?对不起,事实上这是正确的,样本输出的顺序不同,并且没有注意到副本实际上应该在那里。谢谢你的解决方案。