2011-03-21 68 views
1

我收到“缺少右括号错误,同时运行此查询”帮助与Oracle查询(缺少右paranethesis)

select a.session_id, 
     a.request_id, 
     a.timestamp, 
     a.queue_tag, 
     b.* 
    from (select session_id, 
       request_id, 
       timestamp, 
       queue_tag, 
       (select min(b.timestamp) nextrec 
        from tbl b 
       where a.session_id = b.session_id 
        and a.request_id = b.request_id 
        and b.timestamp > a.timestamp 
        and b.is_queue_empty = 0 
       ) 
      from tbl a 
     where is_queue_empty = 1 
      and nullif(queue_name,'') is null 
     ) a 
     left join tbl b 
    on a.session_id = b.session_id 
    and a.request_id = b.request_id 
    and a.nextrec = b.timestamp 

它是有效的,选择这样的Oracle列值?如果不是我在这里错过了什么?

+0

如果你改变'和b.is_queue_empty = 0) 'to'和b.is_queue_empty = 0)作为nextrec'? – 2011-03-21 18:10:01

+0

此外,您在底部 – 2011-03-21 18:19:50

回答

3

我上面的查询工作,只好搬到列别名出来(由@马丁的建议),除去多余的NULLIF()

with tbl as(
      select SYSTIMESTAMP+1 timestamp, 0 is_queue_empty , '' queue_name , 'qt' queue_tag, -1 session_id , 1 request_id from dual 
      union all 
      select SYSTIMESTAMP timestamp, 1 is_queue_empty , '' queue_name , 'qt1' queue_tag, -1 session_id , 1 request_id from dual 
      union all 
      select SYSTIMESTAMP+1 timestamp, 0 is_queue_empty , '' queue_name , 'qt2' queue_tag, -2 session_id , 2 request_id from dual 
      union all 
      select SYSTIMESTAMP timestamp, 1 is_queue_empty , '' queue_name , 'qt22' queue_tag, -2 session_id , 2 request_id from dual    
) 
select a.session_id,a.request_id,a.timestamp,a.queue_tag, 
    b.* 
from 
(
    select session_id,request_id,timestamp,queue_tag, 
    (select min(b.timestamp) nextrec 
     from tbl b 
     where a.session_id=b.session_id 
     and a.request_id=b.request_id 
     and b.timestamp > a.timestamp 
     and b.is_queue_empty=0) nextrec --> had to put this outside the loop 
    from tbl a 
    where is_queue_empty=1 and queue_name is null --in oracle empty string is null thus nullif(queue_name,'') is redundant 
) a 
left join tbl b on a.session_id=b.session_id 
       and a.request_id=b.request_id 
       and a.nextrec = b.timestamp 
+0

的JOIN中没有使用正确的别名('aleft'),对我无效。我仍然得到同样的错误。 – TopCoder 2011-03-21 18:57:20

+0

@topCoder,你在什么版本的Oracle?此外,以上(与'与子句'查询)工作? – Harrison 2011-03-21 19:06:44

0

你只需要添加别名NEXTREC您的标后子查询。您在该查询中指定名称,但是Oracle在左连接中加入时不知道其列名。这应该工作:

select a.session_id, 
     a.request_id, 
     a.timestamp, 
     a.queue_tag, 
     b.* 
    from (select session_id, 
       request_id, 
       timestamp, 
       queue_tag, 
       (select min(b.timestamp) nextrec 
        from tbl b 
       where a.session_id = b.session_id 
        and a.request_id = b.request_id 
        and b.timestamp > a.timestamp 
        and b.is_queue_empty = 0 
       ) nextrec 
      from tbl a 
     where is_queue_empty = 1 
      and nullif(queue_name,'') is null 
     ) a 
     left join tbl b 
    on a.session_id = b.session_id 
    and a.request_id = b.request_id 
    and a.nextrec = b.timestamp 

此外,当我上运行的Oracle 10g原始查询我从数据库中获取以下更多的描述性错误:

and a.nextrec = b.timestamp 
      * 
ERROR at line 44: 
ORA-00904: "A"."NEXTREC": invalid identifier