2017-06-14 91 views
0

我有一個Oracle視圖,需要用它來填充頁面上的不同名稱。使用相同的數據庫列根據搜索條件填充頁面的名稱。分配給名稱的值僅取決於搜索條件。爲了使這個更具體,在這裏是一個例子Oracle SQL基於同一表中的條件檢索列

EMPLOYEE_ID DEPT ROLE_TYPE  GROUP_TYPE 
123   IT  DEVELOPER  SUPPORT 
111   IT  DEVELOPER  APPL_SERVICES 
145   IT  QA    APPL_SERVICES 
222   IT  WEB    APPL_SERVICES 
322   IT  WEB    SUPPORT 

的字段名稱/分類由該表來填充如下

 WEB SUPPORT SERVICES 
    WEB APPLICATION SERVICES 
    DEVELOPER APPLICATION SERVICES 
    DEVELOPER SUPPORT SERVICES 
    QA APPLICATION SERVICES 
    QA SUPPORT SERVICES 

的邏輯是使用SQL濾波器如下:

where ROLE_TYPE='DEVELOPER' AND GROUP_TYPE='APPL_SERVICES' 
Then assign employee 111 to DEVELOPER APPLICATION SERVICES 

where ROLE_TYPE='DEVELOPER' AND GROUP_TYPE='SUPPORT' 
Then assign employee 123 to DEVELOPER SUPPORT SERVICES 

where ROLE_TYPE='WEB' AND GROUP_TYPE='SUPPORT' 
Then assign employee 322 to WEB SUPPORT SERVICES 

where ROLE_TYPE='WEB' AND GROUP_TYPE='APPL_SERVICES' 
Then assign employee 222 to WEB APPLICATION SERVICES 

and so on.. 

我的問題是什麼是最好的方式來構建查詢,而不必做多個查詢?我不想爲每個條件編寫單獨的查詢。

爲了澄清我的意思是,這裏的UI顯示會是什麼樣子:

USER   FUNCTION   POSITION   GROUP 







123  DEVELOPER SUPPORT SERVICES   DEVELOPER  SUPPORT SERVICES 

謝謝

+0

您是什麼意思「將員工111分配給開發人員應用程序服務」。你的意思是你想更新你的表上的GROUP_TYPE列嗎? –

+0

我的意思是顯示員工編號功能角色 – BreenDeen

回答

1

你只需要使用case

select t.*, 
     (case when ROLE_TYPE = 'DEVELOPER' AND GROUP_TYPE = 'APPL_SERVICES' 
      then 'DEVELOPER APPLICATION SERVICES' 
      when ROLE_TYPE ='DEVELOPER' AND GROUP_TYPE = 'SUPPORT' 
      then 'DEVELOPER SUPPORT SERVICES' 
      when ROLE_TYPE = 'WEB' AND GROUP_TYPE = 'SUPPORT' 
      then 'WEB SUPPORT SERVICES' 
      when ROLE_TYPE = 'WEB' AND GROUP_TYPE = 'APPL_SERVICES' 
      then 'WEB APPLICATION SERVICES' 
     end) as new_group 
from . . . 
+0

非常感謝! – BreenDeen

0

DECODE功能會比較每列值逐一。如果您想根據一條語句進行比較,則可以使用它:

select 
    a.*, 
    decode(a.employee_id, 
    123,'DEVELOPER SUPPORT SERVICES', 
    111,'DEVELOPER APPLICATION SERVICE', 
    145,'QA Application services', 
    222,'WEB Application service', 
    322,'WEB SUPPORT SERVICES' 
) as new_group 
from table_name a;