200分:求三层内联查询的难题
我在一个贴只能发100分,所以我将发了两个贴,内容一样的:
三个表都是一对多的关系:   task_person_role -> task 
   task -> result现在我想执行这个查询:
select (
  select count(*) from 
  ( 
    select t.*, 
    (
      select min(action_date)  from task_person_role  where task_id=t.task_id   
    ) due_date2 from task t 
    WHERE  result_id =r.result_id
  ) 
) amount
from result where result_id=112371oracle 9总是抱怨  ORA-00904: "R"."RESULT_ID": invalid identifier请问我如何将最外层的result_id传递到最里面?现在的这个sql已经作了一些简化,真实的sql在每层都加了一些条件。

解决方案 »

  1.   

    try:
    ------------------------------------------------------------------------
    select 
        (select 
             count(*) 
         from 
             (select 
                  t.*, 
                  (select 
                       min(action_date) 
                   from
                       task_person_role 
                   where
                       task_id=t.task_id   
                   ) due_date2 
              from 
                  task t) b
         where
              b.result_id =r.result_id) amount
    from 
        result r
    where 
        r.result_id=112371
      

  2.   

    更正一下,sql语句应该是这样写的,我漏写了一个r:select (
    select count(*) from
    (
    select t.*,
    (
    select min(action_date) from task_person_role where task_id=t.task_id
    ) due_date2 from task t
    WHERE result_id =r.result_id
    )
    ) amount,r.* 
    from result r where result_id=112371为什么要内联查询呢? 原始的语句需要对amount加上where 条件和排序。根纪录也不是一条。
      

  3.   

    为了让大家把这个问题看得更清楚一些,我重新简化了一下问题,变成两个表:
    task -> result
    语句如下:SELECT r.result_id,
    (
    select count(*) from
    (
    select create_date
    from task t
    WHERE result_id =r.result_id
    )
    WHERE create_date <=(SYSDATE+10)
    ) urgency_count
    FROM result r
    WHERE result_id in(112371,112459)需要注意的是create_date 本身又是一个内联产生的虚字段,所以我不能把第二层和第三层合在
    一起。