我正在编写一个查询来提取员工和帐户信息。我保留了一个运行历史记录,每个员工在一个accounthistory表中没有重叠日期。我基本上试图提示我的用户他们想要该员工的员工/帐户的日期。因此,根据用户在运行时选择的日期选项,我需要我的连接是动态/条件的。这是我正在使用的当前查询,它在第3行#51列上返回一个缺少的表达式错误。我仍在四处寻找,但我想我会问这里,以防有人已经取得了类似的东西。
(用户将通过应用程序填充:DATEOPTION变量。应用程序自动填充任何其他变量)
SELECT EMP.employeeid, ACT.accountname FROM employee EMP LEFT OUTER JOIN accounthistory AHI ON (EMP.employeeid = AHI.employeeid AND CASE :DATEOPTION WHEN 1 THEN EMP.ppstartdatedtm BETWEEN AHI.effectivedtm AND AHI.expirationdtm WHEN 2 THEN EMP.ppenddatedtm BETWEEN AHI.effectivedtm AND AHI.expirationdtm WHEN 3 THEN EMP.cpstartdatedtm BETWEEN AHI.effectivedtm AND AHI.expirationdtm WHEN 4 THEN EMP.cpenddatedtm BETWEEN AHI.effectivedtm AND AHI.expirationdtm WHEN 5 THEN TO_DATE(:STARTDATETIME,'MM/DD/YYYY HH24:MI:SS') BETWEEN AHI.effectivedtm AND AHI.expirationdtm WHEN 6 THEN TO_DATE(:ENDDATETIME,'MM/DD/YYYY HH24:MI:SS') BETWEEN AHI.effectivedtm AND AHI.expirationdtm END) INNER JOIN account ACT ON (AHI.accountid = ACT.accountid
答案 0 :(得分:2)
Oracle不支持case
表达式可以返回的“boolean”类型。无论如何,不需要case
:
ON EMP.employeeid = AHI.employeeidCASE AND
( (:DATEOPTION = 1 AND EMP.ppstartdatedtm BETWEEN AHI.effectivedtm AND AHI.expirationdtm) OR
(:DATEOPTION = 2 AND EMP.ppenddatedtm BETWEEN AHI.effectivedtm AND AHI.expirationdtm) OR
. . .
)