此Oracle查询有什么问题。我正在尝试嵌套的case表达式,但无法找到查询失败的原因。
SELECT USER_ID,
ADVISER_FORENAME,
ADVISER_SURNAME,
AGENT_CODE,
'ABC@WIPRO.COM' AS EMAIL_ADDRESS,
SUBMISSION_DATE,
STATUS_CHANGED_DATE,
CASE
WHEN (
CASE
WHEN TO_CHAR(TRUNC(status_changed_date), 'DY') IN ('fri','sat','sun')
THEN ((TRUNC(STATUS_CHANGED_DATE) - (TRUNC(SUBMISSION_DATE)))>4)
ELSE ((TRUNC(STATUS_CHANGED_DATE) - (TRUNC(SUBMISSION_DATE)))>6)
END)
THEN 'B'
ELSE 'A'
END AS CATEGORY_CODE,
PLAN_REF,
PRODUCT_CODE,
CASE
WHEN ((TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE))>4)
THEN 'IPP'
ELSE 'ICP'
END AS TOUCHPOINT_NAME
FROM PIPELINE_PLAN
WHERE STATUS =26
AND PRODUCT_TYPE =1
AND (TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE))<=4;
当我尝试跑步时,会出现以下错误:
ORA-00907:缺少右括号
00907. 00000 - “缺少右括号”
*原因:
*行动:
行错误:53列:84
我检查了缺失的括号,但没有找到。
请帮忙。
答案 0 :(得分:2)
你的嵌套CASE
有点太聪明了。在oracle中,比较(产生3向布尔结果TRUE/FALSE/UNKNOWN
)和表达式(产生其他类型的值)是不可互换的。
CASE
表达式的结果不是布尔值,因此它不能用作外WHEN
中CASE
的参数,这需要布尔值。< / p>
您的查询中有这样的结构:
CASE
WHEN (
CASE
WHEN x IN ('1','2','3')
THEN (y>4)
ELSE (y>6)
END)
THEN 'B'
ELSE 'A'
END
需要重写为
CASE
WHEN y > (CASE WHEN x IN ('1','2','3') THEN 4 ELSE 6 END)
THEN 'B'
ELSE 'A'
END
或
CASE
WHEN (x IN ('1','2','3') AND y>4) OR (x NOT IN ('1','2','3') AND y>6)
THEN 'B'
ELSE 'A'
END
答案 1 :(得分:0)
我没有看到括号有问题,但您不能在then
的{{1}}子句中使用布尔值作为结果。也许这个版本可行:
case
此外,根据SELECT USER_ID, ADVISER_FORENAME, ADVISER_SURNAME, AGENT_CODE,
'ABC@WIPRO.COM' AS EMAIL_ADDRESS, SUBMISSION_DATE, STATUS_CHANGED_DATE,
(CASE WHEN (TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 4 and
TO_CHAR(TRUNC(status_changed_date), 'DY') IN ('fri','sat','sun')
) or
(TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 6 and
TO_CHAR(TRUNC(status_changed_date), 'DY') not IN ('fri','sat','sun')
)
THEN 'B'
ELSE 'A'
END) AS CATEGORY_CODE,
PLAN_REF, PRODUCT_CODE,
(CASE WHEN TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 4
THEN 'IPP'
ELSE 'ICP'
END) AS TOUCHPOINT_NAME
FROM PIPELINE_PLAN
WHERE STATUS = 26 AND PRODUCT_TYPE = 1 AND
TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) <= 4;
子句,条件where
永远不会成立。