我试图返回官员名称,他们管理的酒店以及他们从以下数据库中查看的匹配数量(请参阅下面的关系图)。
我尝试了以下操作,但它不起作用:
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
语句也不起作用。
参考的关系图:
答案 0 :(得分: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, '-')