簡體   English   中英

Oracle-根據表2中的行值在表1中選擇列值作為行

[英]Oracle - Selecting column values as rows in table 1 based on row value in table 2

桌子

我想選擇一個名稱列表作為輸出。 我要為所有wotranstype都包含sitegm的名稱, 並且僅當存在budchg的wotranstype時包含execmgr的名稱,而僅對於計划 的wotranstype包含prodmgr的名稱。

所以到目前為止,我需要的結果集應該顯示

  1. 射線
  2. 查爾斯

因為沒有計划的交易類型。

我嘗試使用帶有union的case語句並按以下條件有條件地獲取它們。

  (  
 SELECT   
 CASE WHEN t.wotranstype = 'budchg'  
 THEN w.SITEGM as recipient  
 else NULL  
 end  
 FROM wotrans T, wonotify w  
 WHERE T.PROJNUM = W.PROJNUM  
 )  
 UNION  
 (  
  SELECT   
  CASE WHEN t.wotranstype = 'planning'  
  THEN w.prodmgr as recipient  
  else NULL  
  end  
  FROM wotrans T, wonotify w  
 WHERE T.PROJNUM = W.PROJNUM  
  )  
UNION  
(...  

應該只是:

select  sitegm
from    wotrans join wonotify using (projnum)
union all
select  execmgr
from    wotrans join wonotify using (projnum)
where   wotranstype = 'budchg'
union all
select  prodmgr
from    wotrans join wonotify using (projnum)
where   wotranstype = 'planning'

我認為您使用UNION的方向正確。 您是否嘗試過將條件放在WHERE部分而不是使用case並使用UNION ALL:

(
SELECT
w.SITEGM as recipient
FROM wotrans T, wonotify w
WHERE T.PROJNUM = W.PROJNUM 
AND t.wotranstype = 'budchg'
)
UNION ALL
(
SELECT 
w.prodmgr as recipient
FROM wotrans T, wonotify w
WHERE T.PROJNUM = W.PROJNUM
AND t.wotranstype = 'planning'
)
UNION ALL
(...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM