加入和条件

时间:2011-04-11 09:32:46

标签: sql oracle plsql

我需要获取活动名称,其中deletedsw ='0'和eventdtm = sysdate-12。但是为了获得活动的全名,我认为它正在进行自我加入。但我不确定如何在这些查询中包含这些条件。任何人都可以请帮助

SELECT  
  (DECODE(levelLess3.activitynm, NULL, '', levelLess3.activitynm || '/') ) || 
 (DECODE(levelLess2.activitynm, NULL, '', levelLess2.activitynm || '/') ) || 
 (DECODE(levelLess1.activitynm, NULL, '', levelLess1.activitynm || '/') ) || 
   wa.activitynm as ACTIVITYFULLNM
  FROM wfaactivity wa
    LEFT OUTER JOIN WFAACTIVITY levelless1 ON (wa.parentid = levelless1.wfaactivityid
   AND levelless1.wfaactivityid != wa.wfaactivityid)
 LEFT OUTER JOIN WFAACTIVITY levelless2 ON (levelless1.parentid = levelless2.wfaactivityid
   AND levelless2.wfaactivityid != levelless1.wfaactivityid)
 LEFT OUTER JOIN WFAACTIVITY levelless3 ON (levelless2.parentid = levelless3.wfaactivityid
   AND levelless3.wfaactivityid != levelless2.wfaactivityid)

我希望包含像

这样的条件
where wa.wfaactivityid=wspan.wfaactivityid
and wspan.deletedsw='0'
and to_char(wspan.eventdtm, 'yyyymmdd') >= to_char(sysdate-12,'yyyymmdd')

wspan是另一个表格,我们正在从中获取deletedsw和eventdtm

是的,我这样给了

SELECT  
  (DECODE(levelLess3.activitynm, NULL, '', levelLess3.activitynm || '/') ) || 
 (DECODE(levelLess2.activitynm, NULL, '', levelLess2.activitynm || '/') ) || 
 (DECODE(levelLess1.activitynm, NULL, '', levelLess1.activitynm || '/') ) || 
   wa.activitynm as ACTIVITYFULLNM
  FROM wfaactivity wa,WFAREPACTYSPAN wspan
  where
  wa.wfaactivityid=wspan.wfaactivityid
and wspan.deletedsw='0'
and to_char(wspan.eventdtm, 'yyyymmdd') >= to_char(sysdate-12,'yyyymmdd')
and
    LEFT OUTER JOIN WFAACTIVITY levelless1 ON (wa.parentid = levelless1.wfaactivityid
   AND levelless1.wfaactivityid != wa.wfaactivityid)
 LEFT OUTER JOIN WFAACTIVITY levelless2 ON (levelless1.parentid = levelless2.wfaactivityid
   AND levelless2.wfaactivityid != levelless1.wfaactivityid)
 LEFT OUTER JOIN WFAACTIVITY levelless3 ON (levelless2.parentid = levelless3.wfaactivityid
   AND levelless3.wfaactivityid != levelless2.wfaactivityid)

但是我收到错误ORA-00920无效的关系运算符

2 个答案:

答案 0 :(得分:1)

您的选择失败,因为您将连接条件放入where子句中。试试这个:

Select (DECODE(levelLess3.activitynm, Null, '', levelLess3.activitynm || '/') ) || 
       (DECODE(levelLess2.activitynm, Null, '', levelLess2.activitynm || '/') ) || 
       (DECODE(levelLess1.activitynm, Null, '', levelLess1.activitynm || '/') ) || 
       wa.activitynm As ACTIVITYFULLNM
  From wfaactivity wa
  Join WFAREPACTYSPAN wspan On wa.wfaactivityid=wspan.wfaactivityid
  Left Outer Join WFAACTIVITY levelless1 On (wa.parentid = levelless1.wfaactivityid
                                             And levelless1.wfaactivityid != wa.wfaactivityid)
  Left Outer Join WFAACTIVITY levelless2 On (levelless1.parentid = levelless2.wfaactivityid
   And levelless2.wfaactivityid != levelless1.wfaactivityid)
  Left Outer Join WFAACTIVITY levelless3 On (levelless2.parentid = levelless3.wfaactivityid
   And levelless3.wfaactivityid != levelless2.wfaactivityid)
 Where wspan.deletedsw='0'
   And TO_CHAR(wspan.eventdtm, 'yyyymmdd') >= TO_CHAR(SYSDATE-12,'yyyymmdd')

修改

您可以按照自己的方式加入所需的任何表格,但必须确定如何加入这些表格。从您的问题和评论看来(对不起,如果不是这样)您不熟悉SQL连接。

如果是这样,我建议你阅读excellent explanation on SQL joins。根据您加入表格的方式,您可能无法获得所需的数据。

HTH

答案 1 :(得分:0)

看起来您正在尝试构建层次结构 - 为什么不使用分层查询呢?

select reverse (sys_connect_by_path (reverse (wf.activitynm), '/') as activityfullnm
  from wfaactivity wf
       inner join wfarepactyspan wspan
          on wa.wfaactivityid = wspan.wfaactivityid
 start with (     wspan.deletedsw = 0 and 
              and wspan.eventdtm >= sysdate - 12)
 connect by nocycle prior wf.wfactivityid = wf.parentid 
 where level <= 4