简体   繁体   中英

Invalid identifier in Oracle 11g query

I have this query, where I'm getting ' ORA-00904: "RA"."TARGETRESOURCEID": invalid identifier ' on this line of query:

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

following is the complete query:

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;

Could anyone please identity the issue as I'm unable to find the problem.

The issue is that columns from the outer query can only be accessed one subquery level down. You are trying to access the outer query column many subquery levels down, hence you're getting that error message.

To avoid the error, you need to rewrite the query so that your subquery is only one level deep - something like this (nb untested, as you didn't provide any sample data, etc):

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;

I haven't included your GROUP BY ra.targetresourceid clause, as there are no aggregates, and your query would fail due to it. Perhaps you meant to use DISTINCT in the select list?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM