Is there a way to give a subquery an alias in Oracle 11g SQL?

后端 未结 3 1548
春和景丽
春和景丽 2021-02-07 04:44

Is there a way to give a subquery in Oracle 11g an alias like:

select * 
from
    (select client_ref_id, request from some_table where message_type = 1) abc,
            


        
相关标签:
3条回答
  • 2021-02-07 05:32

    I don't have an Oracle instance to test with, but what you posted should be valid ANSI-89 JOIN syntax. Here it is in 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
    
    0 讨论(0)
  • 2021-02-07 05:39

    You can give a query a name or alias with CTE’s (Common Table Expressions) aka WITH clause aka by Oracle as Subquery Factoring:

    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;
    
    0 讨论(0)
  • 2021-02-07 05:45

    Your query should be fine.

    An alternative would be:

    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;
    

    I wouldn't be surprised if Oracle rewrote the queries so that the plan would be the same anyway.

    0 讨论(0)
提交回复
热议问题