2017-06-22 31 views
-1

我是一個sql案例的問題,我想檢查一個列值,如果返回true,那麼我想要另一個列值改變,但在'case'中,你只能檢查一列,只改變價值,我如何使它工作?sql case when x =?那麼y =?結束

select f.[film name],f.city,p.[participant name], 
case rt.[type name] 
when 'director' then 'director' else null 
end 'role type' 
from Films f 
join FilmParticipants fp on fp.filmID=f.filmID 
join Participants p on p.participantID=fp.participantID 
join RoleType rt on rt.roleID=fp.roleID 
where f.city in(
select f.city 
from Films f 
group by f.city 
having COUNT(*)>1) 
order by f.city 

它給這個表:

film name | city  | participate name | role type 
Shrek  | London  | John    | null 
Shrek  | London  | John    | null 
Dragon  | London  | Rick    | null 
Drago  | London  | Morty   | Director 

現在我想,只要有在「角色類型列」空的「參與名稱」欄爲空爲好。

+3

更新你的問題添加適當的數據樣本和預期的結果,並告訴空同樣的方式我們你正在使用哪個db – scaisEdge

+0

查詢有什麼問題......? – scaisEdge

+0

我已經編輯了這篇文章,以便更清楚地瞭解 – Eden

回答

0

你可以使用的情況下當角色類型和顯示partecipant.name

 select f.[film name],f.city, 
     case rt.rt.[type name] 
      when 'director' then p.[participant name] else null end 'partecipant', 
     case rt.[type name] 
      when 'director' then 'director' else null 
     end 'role type' 
     from Films f 
     join FilmParticipants fp on fp.filmID=f.filmID 
     join Participants p on p.participantID=fp.participantID 
     join RoleType rt on rt.roleID=fp.roleID 
     where f.city in(
     select f.city 
     from Films f 
     group by f.city 
     having COUNT(*)>1) 

     order by f.city 

用於去除

 select [film name], city, max(partecipant), max([role type]) 
     from (
     select f.[film name],f.city, 
     case rt.rt.[type name] 
      when 'director' then p.[participant name] else null end 'partecipant', 
     case rt.[type name] 
      when 'director' then 'director' else null 
     end 'role type' 
     from Films f 
     join FilmParticipants fp on fp.filmID=f.filmID 
     join Participants p on p.participantID=fp.participantID 
     join RoleType rt on rt.roleID=fp.roleID 
     where f.city in(
     select f.city 
     from Films f 
     group by f.city 
     having COUNT(*)>1) 
     order by f.city) t 
     group by [film name], city 
+0

非常感謝你!這個很棘手。 電影名稱|城市|參加名字|角色類型 史瑞克|倫敦| null | null 龍|倫敦| null | null 龍|倫敦|莫蒂|導演 現在它給了我這張桌子,你碰巧知道我該如何讓第二排消失?如果那個'電影名稱'已經存在,並且它有這些空值,我不需要看到它。 – Eden

+0

@Eden好,如果我的回答是正確的,請將其標記爲已接受...看到這裏如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge

+0

它被接受,我會appriciate你幫助我與我的第二個問題,雖然哪個職位發表評論 – Eden