2015-07-09 83 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 
(... 
相关问题