it-source

오라클 11g SQL에서 하위 쿼리에 별칭을 부여하는 방법이 있습니까?

criticalcode 2023. 7. 5. 20:47
반응형

오라클 11g SQL에서 하위 쿼리에 별칭을 부여하는 방법이 있습니까?

Oracle 11g의 하위 쿼리에 다음과 같은 별칭을 부여하는 방법이 있습니까?

select * 
from
    (select client_ref_id, request from some_table where message_type = 1) abc,
    (select client_ref_id, response  from some_table where message_type = 2) defg
where
    abc.client_ref_id = def.client_ref_id;

그렇지 않으면 client_ref_id를 기반으로 두 하위 쿼리를 결합할 수 있습니다.자가 가입이 있다는 것은 알고 있지만 데이터베이스에서 자가 가입을 실행하는 경우 완료하는 데 최대 5분이 걸릴 수 있습니다(실제 실행 중인 쿼리에는 추가 논리가 있지만 자가 가입이 문제의 원인이라고 판단했습니다).개별 하위 쿼리는 자체적으로 완료하는 데 몇 초밖에 걸리지 않습니다.자체 조인 쿼리는 다음과 같습니다.

select st.request, st1.request
from
    some_table st, some_table st1
where 
    st.client_ref_id = st1.client_ref_id;

쿼리에 CTE(Common Table Expressions)와 함께 이름 또는 별칭을 지정할 수 있습니다(Oracle에서 하위 쿼리 팩터링이라고 함).

WITH abc as (select client_ref_id, request from some_table where message_type = 1)
select * 
from abc
    inner join 
    (select client_ref_id, response  from some_table where message_type = 2) defg
       on abc.client_ref_id = def.client_ref_id;

테스트할 Oracle 인스턴스가 없지만, 당신이 게시한 내용은 유효한 ANSI-89 JOIN 구문이어야 합니다.다음은 ANSI-92입니다.

SELECT *
  FROM (SELECT client_ref_id, request 
          FROM SOME_TABLE 
         WHERE message_type = 1) abc
  JOIN (SELECT client_ref_id, request 
          FROM SOME_TABLE 
         WHERE message_type = 1) defg ON defg.client_ref_id = abc.client_ref_id

당신의 질문은 괜찮을 것입니다.

대안은 다음과 같습니다.

select abc.client_ref_id, abc.request, def.response
from   some_table abc,
       some_table def
where  abc.client_ref_id = def.client_ref_id
and    abc.message_type = 1
and    def.message_type = 2;

Oracle이 쿼리를 다시 작성하여 계획이 동일하도록 해도 놀라지 않을 것입니다.

SQL 표준에서 하위 쿼리에 대한 별칭을 다음과 같이 정의할 수 있습니다.

Temp.pente, Temp를 선택합니다.살라리오 프롬
From(A.aid aid, A.pente as patente, 특허권자 선택),
AVG(E.esalario) AS Salario Prom
항공기 A, 인증 C, 직원 E로부터
A.aid = C.aid 위치
AND C.eid = E.eid
AND A.rangocruceo > 1000
A.aid, A.pente )를 온도로 그룹화

여기서 Temp는 하위 쿼리의 별칭입니다.

Oracle에서는 특히 하위 쿼리가 복잡하고 많은 임시 공간을 사용하는 경우 WITH 절을 사용할 수 있습니다.

Oracle에서는 다음 구문이 제대로 작동합니다.

Temp.pente, Temp를 선택합니다.살라리오 프롬
From(A.aid aid, A.pente as patente, 특허권자 선택),
AVG(E.esalario) AS Salario Prom
항공기 A, 인증 C, 직원 E로부터
A.aid = C.aid 위치
AND C.eid = E.eid
AND A.rangocruceo > 1000
A.aid, A.pente를 기준으로 그룹화)

언급URL : https://stackoverflow.com/questions/3057930/is-there-a-way-to-give-a-subquery-an-alias-in-oracle-11g-sql

반응형