0

我认为错误描述有点误导,因为我看不出查询结束的方式有什么问题。我正在尝试加入三个表,但这是行不通的。有人可以指出我做错了什么吗?顺便说一句,这是在 Oracle 上。

SELECT s.PITIMEDATE, ROUND(SYSDATE-s.PITIMEDATE) DIFFDAYS, s.SRC, s.VSA6, a.VSA9, c.COMMENTS, c.Actions, c.MYID 
FROM alarm_standing_MAU AS s 
INNER JOIN alarm_MAU AS a 
    ON a.SRC = s.SRC 
    AND a.VSA9 = 
    (SELECT VSA9 
        FROM a 
        WHERE a.SRC = s.SRC 
        AND ROWNUM=1) 
INNER JOIN Alarm_Standing_Comments AS c 
    ON c.SRC = s.SRC 
AND s.SRC<>'LastCheck' 
GROUP BY s.SRC, s.PITIMEDATE, s.VSA6, a.VSA9, c.Comments, c.Actions, c.MYID 
ORDER BY s.PITIMEDATE ASC

有关更多信息,我试图向查询添加第二个连接,但它变得有点笨拙,因此正在尝试使用别名。这是最初的查询 - 我想知道如何通过别名提高可读性。

SELECT alarm_standing_MAU.PITIMEDATE, ROUND(SYSDATE-alarm_standing_MAU.PITIMEDATE) DIFFDAYS, alarm_standing_MAU.SRC, alarm_standing_MAU.VSA6, alarm_MAU.VSA9 
FROM alarm_standing_MAU 
INNER JOIN alarm_MAU 
ON alarm_MAU.SRC = alarm_standing_MAU.SRC 
AND alarm_MAU.VSA9 = 
    (SELECT VSA9 
    FROM alarm_MAU 
    WHERE alarm_MAU.SRC = alarm_standing_MAU.SRC 
    AND ROWNUM=1) 
AND alarm_standing_MAU.SRC<>'LastCheck' 
GROUP BY alarm_standing_MAU.SRC, alarm_standing_MAU.PITIMEDATE, alarm_standing_MAU.VSA6, alarm_MAU.VSA9 
ORDER BY alarm_standing_MAU.PITIMEDATE ASC
4

2 回答 2

0

作为提示,您不能在 from 子句中使用别名:

INNER JOIN alarm_MAU AS a 
    ON a.SRC = s.SRC 
    AND a.VSA9 = 
    (SELECT VSA9 
        FROM a                           <-- Oops.  Might try "FROM alarm_MAU as b" ?
        WHERE a.SRC = s.SRC 
        AND ROWNUM=1)

我不确定您要完成什么,但这就是我要开始的地方。

于 2013-04-26T09:08:53.740 回答
0

如果子查询使用与主查询相同的表,那么您可以在主查询中使用 a1,在子查询中使用 a2,例如

SELECT s.PITIMEDATE, 
       ROUND(SYSDATE-s.PITIMEDATE) DIFFDAYS, 
       s.SRC, 
       s.VSA6, 
       a1.VSA9 
FROM alarm_standing_MAU s
  INNER JOIN alarm_MAU a1
  ON a1.SRC = s.SRC 
  AND a1.VSA9 = 
    (SELECT a2.VSA9 
     FROM   alarm_MAU a2
     WHERE  a2.SRC = s.SRC 
     AND    ROWNUM=1) 
  AND s.SRC<>'LastCheck' 
GROUP BY s.SRC, 
         s.PITIMEDATE, 
         s.VSA6, 
         a1.VSA9 
ORDER BY s.PITIMEDATE ASC
于 2013-04-26T10:47:11.077 回答