Oracle 11g查询中的标识符无效

时间:2019-05-16 11:00:50

标签: database oracle11g

我有此查询,在以下查询行上得到“ ORA-00904:“ RA”。“ TARGETRESOURCEID”:无效标识符

t2.Id IN (SELECT TaskId FROM ResourceAssignment WHERE TargetResourceId = ra.TargetResourceId)

以下是完整的查询:

Select ra.TargetResourceId, 
(
  SELECT gn.Name FROM GeoNode gn WHERE gn.Id = 
  (
    SELECT t.StartPointId
    FROM Task t 
    WHERE t.Id = 
    (
      SELECT Id FROM
        (SELECT 
          t2.Id
        From 
          Task t2
        WHERE 
          t2.Id IN (SELECT TaskId FROM ResourceAssignment WHERE TargetResourceId = ra.TargetResourceId)
        ORDER BY 
          t2.StartTime1)
      WHERE rownum = 1 
    )
  ) 
) as DepartureNodeName
FROM 
  ResourceAssignment ra
INNER JOIN 
  Vehicle v ON v.Id = ra.SourceResourceId
INNER JOIN
  Unit u ON v.UnitId = u.Id
GROUP BY
    ra.TargetResourceId;

由于我无法找到问题,因此任何人都可以标识该问题。

1 个答案:

答案 0 :(得分:3)

问题在于,外部查询中的列只能向下访问一个子查询级别。您正在尝试访问许多子查询级别较低的外部查询列,因此会收到该错误消息。

为避免错误,您需要重写查询,以使子查询仅深入一层-诸如此类(n.b.未经测试,因为您未提供任何示例数据,等等):

WITH assignment_info AS (SELECT ra.targetresourceid,
                                (SELECT MIN(t.startpointid) KEEP (dense_rank FIRST ORDER BY t.starttime1) task_startpointid
                                 FROM   task t
                                 WHERE  t.id = ra.targetresourceid) task_startpointid
                         FROM   resourceassignment ra
                         INNER  JOIN vehicle v
                         ON     v.id = ra.sourceresourceid
                         INNER  JOIN unit u
                         ON     v.unitid = u.id)
SELECT ai.targetresourceid,
       (SELECT gn.name
      FROM   geonode gn
      WHERE  gn.id = ai.task_startpointid) departurenodename
FROM   assignment_info;

我没有包含您的GROUP BY ra.targetresourceid子句,因为没有聚合,因此您的查询将失败。也许您打算在选择列表中使用DISTINCT