2015-07-09 79 views
0

tablesOracle - 根據表2中的行值在表1中選擇列值

我想選擇一個名稱列表作爲輸出。我想包括sitegm所有wotranstype的execmgr的名字的名字只有在有budchg的wotranstype和prodmgr 只爲規劃的wotranstype

所以名字作爲now..the結果集的,我需要應顯示

  1. 查爾斯

由於沒有交易類型的計劃。

我嘗試使用聯合使用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 
(... 
+1

請顯示您嘗試過的選擇。 – OldProgrammer

+0

不相關,但:每個選擇周圍的括號是不必要的(並且是無用的) –

回答

0

應該僅僅是:

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

如果您仔細看到有兩個表格提到 – Aravindh

+0

@Aravindh請參閱編輯並添加了連接。我認爲選擇列表中和你的情況中的列都來自wotrans,這將使第二個表無用。在附註中,您應該使用join子句來加入連接條件,而不是where子句。 –

0

我認爲你是使用UNION在正確的軌道上。您是否嘗試將條件放在WHERE部分而不是使用大小寫,並使用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 
(... 
相關問題