2013-05-10 40 views
0

我試圖從下面的數據庫(參見下面的關係圖)中返回他們管理的官員姓名,他們管理的酒店以及他們已經參考的匹配數量。無法讓這個查詢在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 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 

我得到一組功能的錯誤選擇,並在年底的case聲明也不起作用。從參考

關係圖:

enter image description here

回答

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, '-') 
+0

DBMS是Oracle。 – Martin 2013-05-10 09:19:17

+0

@Tangler試試這個 – 2013-05-10 09:20:32

+0

這幾乎是正確的,只是它有時會返回官方的複製記錄(並且將其錯誤地分配給管理酒店)。 – Martin 2013-05-10 09:25:55

相關問題