加入2个别名表

时间:2016-12-08 11:36:00

标签: sql oracle join

我有3个snipets代码。 我想要的是加入2个表。我想到了加入这样的事情。

with CH_AL_SDB as
(
  select sr13.*, sr14.*
  from sr13, sr14
  where sr14.SUBS_KEY = sr13.S_CTN_MAIN
)select * from CH_AL_SDB;

我在这里从一张桌子中选择。

with sr13 as
(
  select distinct CTN_MAIN, SUBS_KEY, BAN_KEY, count(CTN_MAIN) as CTN_QUANTITY
  from ETL1_ETL.STG_SDB_LOAD SDB
  group by CTN_MAIN, SUBS_KEY, BAN_KEY
)select * from sr13;

这是第二张表。

with sr14 as
(
  select FPCN.*
  from FCT_PREP_CHARGES_N FPCN
    where FPCN.business_service_key = 33006
    and CHARGE_TYPE='R'
)select * from sr14;

别名不是必需的。到目前为止,这是我唯一能想到的东西。执行后有39列,问题是sr14。 sr13有4.结果中应该有43个,但是有35个。当我选择所有3个查询并在sql developer中运行它们时。来自sr13的专栏根本没有出现。并且sr14中有4列缺失。来自sr13和sr14的缺失列的名称不同。

在sqldeveloper中,订单是

CH_AL_SDB
sr13
sr14

当我将其更改为

CH_AL_SDB
sr14
sr13

只显示sr13的4列。

1 个答案:

答案 0 :(得分:0)

以下是我解决这个问题的方法

with CH_AL_SDB as
(
    select distinct SDB.CTN_MAIN as S_CTN_MAIN, 
      SDB.SUBS_KEY as S_SUBS_KEY, 
      SDB.BAN_KEY as S_BAN_KEY, 
      count(SDB.CTN_MAIN) as CTN_QUANTITY,
      FPCN.BAN_KEY
    from STG_SDB_LOAD SDB
    inner join FCT_PREP_CHARGES_N FPCN
    on FPCN.SUBS_KEY = SDB.CTN_MAIN
    group by CTN_MAIN, 
      SDB.SUBS_KEY, 
      SDB.BAN_KEY, 
      FPCN.BAN_KEY
)select * from CH_AL_SDB;

在我的小组中,我添加了FPCN.BAN_KEY以缩短它,但我需要列出所有该死的字段。

更新

这是一种更好的方法

with sr13 as
(
  select distinct CTN_MAIN, SUBS_KEY, BAN_KEY, count(CTN_MAIN) as CTN_QUANTITY
  from ETL1_ETL.STG_SDB_LOAD SDB
  group by CTN_MAIN, SUBS_KEY, BAN_KEY
),
sr14 as
(
  select FPCN.*
  from FCT_PREP_CHARGES_N FPCN
    where FPCN.business_service_key = 33006
    and CHARGE_TYPE='R'
)
  select sr13.*, sr14.*
  from sr13, sr14
  where sr14.SUBS_KEY = sr13.S_CTN_MAIN