0

我试图从下面的数据库中返回官员姓名、他们管理的酒店以及他们推荐的比赛数量(参见下面的关系图)。

我尝试了以下方法,但它不起作用:

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 的组函数错误,case最后的语句也不起作用。

参考关系图:

在此处输入图像描述

4

1 回答 1

1

你的 DBMS 是什么?

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, '-')
于 2013-05-10T09:17:33.293 回答