[oracle]在 SQL 中的 IF/ELSE 条件

标签: sql Oracle
发布时间: 2017/4/15 18:48:13
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想做 IF/ELSE 语句 PLSQL 但我不确定如果我在做它以正确的方式,因为它总是有一个错误︰ 上缺少右括号如果 (TO_NUMBER (SUBSTR (ATTR_VALUE,6,2)) ! = TO_NUMBER (SUBSTR (ATTR_VALUE,1,2))) 然后但括号似乎对我的平衡。

SELECT *
FROM
(
IF (TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 2)) != TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 2))) THEN
  SELECT ID, DATE, ATTR_VALUE, (TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 4))-TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 4))-48) DIFF
ELSE
  SELECT ID, DATE, ATTR_VALUE, (TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 4))-TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 4))) DIFF
END IF
FROM Table A
ORDER BY TIME
)
WHERE DIFF>26

请协助我在这个问题上。

解决方法 1:

你应该能够使用 CASE 语句以获得结果︰

SELECT *
FROM
(
  SELECT ID, 
    DATE, 
    ATTR_VALUE,
    CASE 
      WHEN TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 2)) != TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 2))
      THEN (TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 4))-TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 4))-48)
      ELSE (TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 4))-TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 4)))
    END DIFF
  FROM Table A
  ORDER BY TIME
)
WHERE DIFF>26
官方微信
官方QQ群
31647020