使用Oracle SQL连接表

时间:2019-05-06 08:25:05

标签: oracle

我是Oracle SQL的新手。我希望加入2张桌子。

在表1中,两个表已与以下演示文稿结合在一起

image of table 1

这是表1的sql脚本:

select a.ID,
       a.CLASSIFICATION_CODE,
       a. CATEGORY_CODE
        from a, b
        where locality = 'xxx'   
        and a.DIV = b.DIV
        and a.TYPE = b.TYPE
        and a.DIST = b.DIST
        and a.BS = b.BS
        and a.LOT = b.LOT; 

表2

image of table 2

这是表2的sql脚本:

select CODE_TYPE,CODE_1,CODE_DESC from PUBCODE01
        where PUBCODE01.code_type IN ('LCL','LCA');

表3(连接3个表后的最终结果表)

image of table 3

我需要有关如何结合两个sql脚本以产生表3表示形式的帮助。

2 个答案:

答案 0 :(得分:0)

您可能需要根据code_ty联接同一张表两次;例如:

  it('should download Excel ', () => {
    const expectedResult: ArrayBuffer = new ArrayBuffer(8);
    // httpClientSpy.post.and.returnValue(asyncData(expectedResult));
    httpClientSpy.post.and.returnValue(of({body: expectedResult})); // Or that below one if "asyncData" return Observable

    metricsService.getExcelExportCPPMetrics(resultsFilterRootObject).subscribe(
      data => expect(data.body).toEqual(expectedResult, 'expected VehicleSalesResultRootObject'),
      fail
    );
    expect(httpClientSpy.post.calls.count()).toBe(1, 'one call');
  });

甚至以下内容,具体取决于您的数据是什么:

SQL> with query1(id, classification_code, category_code) as(
  2      select 123, 1, 3 from dual union all
  3      select 456, 2, 4 from dual union all
  4      select 789, 1, 3 from dual
  5  ),
  6  query2(code_type, code_1, code_desc) as (
  7      select 'LCL', 1, 'TOMATOES'  from dual union all
  8      select 'LCL', 2, 'DURIAN'    from dual union all
  9      select 'LCA', 3, 'VEGETABLE' from dual union all
 10      select 'LCA', 4, 'FRUITS'    from dual
 11  )
 12  select id, LCL.code_desc, LCA.code_desc
 13  from query1
 14      inner join query2 LCL
 15          on(LCL.code_1 = query1.classification_code)
 16      inner join query2 LCA
 17          on(LCA.code_1 = query1.category_code);

        ID CODE_DESC CODE_DESC
---------- --------- ---------
       123 TOMATOES  VEGETABLE
       789 TOMATOES  VEGETABLE
       456 DURIAN    FRUITS

顺便说一句,您最好使用ANSI连接;您的查询应该是:

select id, LCL.code_desc, LCA.code_desc
from query1
    inner join query2 LCL
        on(LCL.code_1 = query1.classification_code
           and LCL.code_type = 'LCL')
    inner join query2 LCA
        on(LCA.code_1 = query1.category_code
           and LCA.code_type = 'LCA')

答案 1 :(得分:0)

西红柿是一种水果,请在发布前研究您的产品。