oracle 用于连接表的SQL查询

jhkqcmku  于 2022-12-11  发布在  Oracle
关注(0)|答案(1)|浏览(147)

I need help with SQL.I need to join 2 tables and fetch particular information. Below is the sql/scenario for reference.

**Input:**Sql1-

select distinct schema,tablename,tbl_cmmt as tabledescription 
from test.a
where tbl_cmmt is not null
and
schema is not null

Input data:
schema,tablename,tbl_cmmt

test,sample,test table test1,sample1,test table 2

Sql2-

select schema,table,description from test_descr
join test_class on test_descr.objoid = test_class.oid
join test_namespace on test_class.relnamespace = test_namespace.oid
where kind ='r'

Input data:
schema, table, description

test, sample, table test5, abc , not in use
Output : I want to perform left join on these two sql and fetch all the data from sql1 along with one extra field that is description from sql2.Keys on both tables are schema and table.

Output data
schema, table, tbl_cmmt, description

test, sample, test table, table test1, sample1, test table 2, NULL

gxwragnw

gxwragnw1#

一个简单的选项是将当前查询用作CTE(或子查询),并在公共列上联接它们:

with
query_1 as
  (select distinct schema,tablename,tbl_cmmt as tabledescription 
   from test.a
   where tbl_cmmt is not null
     and schema is not null
  ),
query_2 as
  (select schema,table,description 
   from test_descr
      join test_class on test_descr.objoid = test_class.oid
      join test_namespace on test_class.relnamespace = test_namespace.oid
   where kind ='r'
  )
select a.*, b.description
from query_1 a join query_2 b on a.schema = b.schema and a.tablename = b.table;

相关问题