我无法让INNER JOIN工作" ORA 00918"

时间:2017-07-21 11:01:36

标签: sql oracle inner-join

我有一个查询返回我需要的所有正确的行,如下所示:

SELECT sequence_no,
MAX(time)
FROM ensapp.shop_order_analysis
WHERE analyzer='SORVI01'
AND part_no in ('PTS228AL','PSS1285')
AND sequence_no in ('6','9','*')
AND SYSDATE-15<time group by sequence_no

但是我希望它再返回一个列,当我尝试进行INNER JOIN时,它会说&#34; ORA-00918:列模糊定义&#34;。我试图谷歌这个,但无法理解它。这就是我试过的:

SELECT part_no, sequence_no, time
FROM ensapp.shop_order_analysis a
INNER JOIN(
SELECT sequence_no,
MAX(time)
FROM ensapp.shop_order_analysis
WHERE analyzer='SORVI01'
AND part_no in ('PTS228AL','PSS1285')
AND sequence_no in ('6','9','*')
AND SYSDATE-15<time group by sequence_no) c 
ON a.sequence_no=c.sequence_no

我是SQL的初学者。提前致谢

1 个答案:

答案 0 :(得分:2)

首先,始终限定您的列名称。那你永远不会遇到这个问题:

SELECT a.part_no, a.sequence_no, a.time
FROM ensapp.shop_order_analysis a INNER JOIN
     (SELECT sequence_no, MAX(time) as maxt
      FROM ensapp.shop_order_analysis
      WHERE analyzer = 'SORVI01' AND
            part_no in ('PTS228AL','PSS1285') AND
            sequence_no in ('6', '9', '*') AND
            SYSDATE-15 < time
      GROUP BY sequence_no
     ) aa
     ON a.sequence_no = aa.sequence_no;

我猜你真的想要:

SELECT a.*, MAX(a.time) OVER () as maxt
FROM ensapp.shop_order_analysis s
WHERE analyzer = 'SORVI01' AND
      part_no in ('PTS228AL','PSS1285') AND
      sequence_no in ('6', '9', '*') AND
      SYSDATE - 15 < time;