1

我正在尝试为 PLSQL 执行 IF/ELSE 语句,但我不确定我是否以正确的方式执行此操作,因为它总是出现错误:缺少右括号IF (TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 2)) != TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 2))) THEN 但括号对我来说似乎是平衡的。

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

请在这个问题上帮助我。

4

2 回答 2

5

您应该能够使用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
于 2013-01-11T12:07:44.017 回答
0

您可以将查询修改为如下所示的行,

SELECT *
  FROM (SELECT ID,
               DATE,
               ATTR_VALUE,
               decode(TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 2)),
                      TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 2)),
                      TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 4)) -
                      TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 4)),
                      TO_NUMBER(SUBSTR(ATTR_VALUE, 6, 4)) -
                      TO_NUMBER(SUBSTR(ATTR_VALUE, 1, 4))) DIFF
          FROM A)
 WHERE diff > 26
于 2013-01-11T12:11:56.297 回答