在不同的外键上连接第8次表以从第三个表中获取相同的列数据

时间:2015-11-30 23:27:40

标签: sql oracle join oracle-sqldeveloper

我尝试搜索此内容但找不到答案。对不起,如果有任何答案可用。请指导我,我会读它。

对于一家房地产公司而言,这些公司的交易经历了不同的阶段,员工会在这些交易中验证数据并将其转发给上司。在Oracle中我有四个表可以说是User,Agent,License,Producer。现在用户有关于名称的详细信息,从用户到生产者的唯一连接是通过像这样的代理和许可

Select * from User u
  inner join Agent a on u.primarykey = a.foreignkey
  inner join License l on a.primarykey = l.foreignkey
  inner join Producer p on l.primarykey = p.foreignkey

现在表生产者有8种不同类型的ID,基于生产者的位置。对于特定交易,将涉及不同的人。

我需要一行中参与一笔交易的所有人的名字,所以我必须使用所有不同的ID加入表生产者8次,然后从实体中获取名称。

我对如何在select子句中从实体获取名字第一个名称列有疑问

Select entity.first_name,..........
from User u
     inner join Agent a on u.primarykey = a.foreignkey
     inner join License l on a.primarykey = l.foreignkey
     inner join Producer p1 on l.primarykey = p1.foreignkey1
     inner join Producer p2 on l.primarykey = p2.foreignkey2
     inner join Producer p3 on l.primarykey = p3.foreignkey3
     inner join Producer p4 on l.primarykey = p4.foreignkey4
     inner join Producer p5 on l.primarykey = p5.foreignkey5
     inner join Producer p6 on l.primarykey = p6.foreignkey6
     inner join Producer p7 on l.primarykey = p7.foreignkey7
     inner join Producer p8 on l.primarykey = p8.foreignkey8

需要弄清楚如何编写select子句,以便它在一行中获取有关特定交易的所有名称

添加我实际创建的查询,该查询给出了不同行的结果

   with sel as (
    select
     1 key_type
    from dual union all
    select
     2 key_type
    from dual union all
    select
     3 key_type
    from dual union all
    select
     4 key_type
from dual union all
select
 5 key_type
from dual union all
select
6 key_type
from dual union all
select
7 key_type
from dual
)
select
   case
   when pav.key_type = 1 then e.CLIENT_ID
   else NULL 
   end "Column1",
 case
   when pav.key_type = 1  then e.organization_name
   else NULL 
 end "Name 1",
 case
   when pav.key_type = 2 then LTRIM(RTRIM(e.first_name || ' ' || e.middle_name || ' ' || e.last_name))
   else null
 end "Name3",
case
    when pav.key_type = 3 then LTRIM(RTRIM(e.first_name || ' ' || e.middle_name || ' ' || e.last_name))
   else null
end "Name3",
case
   when pav.key_type = 4 then LTRIM(RTRIM(e.first_name || ' ' || e.middle_name || ' ' || e.last_name))
   else null
 end "Name4",
 case
   when pav.key_type = 5 then LTRIM(RTRIM(e.first_name || ' ' || e.middle_name || ' ' || e.last_name))
   else null
 end "Name5",
  case
   when pav.key_type = 6 then LTRIM(RTRIM(e.first_name || ' ' || e.middle_name || ' ' || e.last_name))
   else null
 end "Name6",
  case
   when pav.key_type = 7 then LTRIM(RTRIM(e.first_name || ' ' || e.middle_name || ' ' || e.last_name))
   else null
 end "Name7"
from
 entity e inner join  agent a
 on  e.entity_pk = a.entity_fk
 inner join license al
 on a.agent_pk = al.agent_fk
 inner join (
   select
     case
       when sel.key_type = 1 then pa.producer_agent_license_fk
       when sel.key_type = 2 then coalesce(pa.subproducer_agent_license_fk,pa.producer_agent_license_fk)
       when sel.key_type = 3 then pa.underwriter_license_fk
       when sel.key_type = 4 then pa.policy_serholder_license_fk
       when sel.key_type = 5 then pa.risk_manager_license_fk
       when sel.key_type = 6 then pa.authorizedrep_agent_license_fk
       when sel.key_type = 7 then pa.asst_underwriter_license_fk
     end license_key
   , sel.key_type
   from
     producer pa
   , sel
   where
       pa.effective_end_date = to_date('01/01/3000','mm/dd/yyyy')
 ) pav
on al.license_pk = pav.license_key;

0 个答案:

没有答案