помочь с запросом Oracle (отсутствует правильный паранетез) - PullRequest
1 голос
/ 21 марта 2011

Я получаю сообщение об ошибке "отсутствует правильная скобка при выполнении этого запроса"

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? Если не то, что мне здесь не хватает?

Ответы [ 2 ]

3 голосов
/ 21 марта 2011

Я получил приведенный выше запрос для работы, пришлось переместить псевдоним столбца (как предложено @Martin) и удалил избыточный 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 голосов
/ 21 марта 2011

Вам просто нужно добавить псевдоним 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...