2011-03-21 6 views
-1

다음 제약 조건을 사용하여 SQL 쿼리를 구성하는 방법은 무엇입니까?SQL 쿼리에 대한 도움말

is_queue_empty = 1이고 queue_name이 비어있는 각 레코드의 경우, 동일한 session-id 및 request-id에 대해 is_queue_empty = 0이고 queue_name이 비어 있거나 될 수없는 timestamp에 의해 다음 레코드를 즉시 확보하십시오.

SESSION_ID, REQUEST_ID, 대기열 이름, is_queue_empty, 타임 스탬프, queue_tag, TAB_NAME :

표는 다음과 같은 열이 있습니다.

내가 dofar이 올바르지 인 이것이다 :

SELECT x.tab_name, 
     x.is_queue_empty, 
     x.SESSION_ID, 
     x.request_ID, 
     x.TO_CHAR(DATETIME, 'YYYY/MM/DD HH24:MI:SS') timestamp, 

     y.tab_name,y.queue_name,y.is_queue_empty 

FROM queue_data AS x 
WHERE 
     timesttamp < TO_DATE('2011/02/30') 
     AND timestamp >= TO_DATE('2011/01/01') 

     AND is_queue_empty=1 

    AND timestamp < (select TO_CHAR(timestamp, 'YYYY/MM/DD HH24:MI:SS') as timestamp from queue_data as Y where x.session_id = y.session_id and x.request_id=y.request_id and y.is_queue_empty=0 order by y.timestamp asc limit 1) 
+0

to_char? 오라클 또는 MySQL입니까? – RichardTheKiwi

+0

oracle.You right – TopCoder

+0

글쎄, 내가 쓴 글과 ANSI 호환 크로스 dbms 대답을 – RichardTheKiwi

답변

2
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) 
     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 

참고 :

  1. tbl가 타임 스탬프를 가정하면 테이블
  2. 의 이름 인 SESSION_ID 내에서 고유, request_id 조합
+0

도와 주셔서 감사합니다. 당신은 진정으로 MySql 전문가입니다. – TopCoder

+0

이 쿼리에서는 "ORA-00907 : right parenthesis missing"오류가 발생합니다. oracle에서 열을 선택할 수 있습니까? – TopCoder

+0

@TopCoder 'nextrec' 열 별칭을 subselect 외부로 옮겼습니다. 오라클과 같이 테스트되지 않았지만 작동해야하는 것처럼 보입니다. – RichardTheKiwi