【问题标题】:joining based on columns priority基于列优先级加入
【发布时间】:2016-06-22 10:59:30
【问题描述】:

我想根据列的优先级加入 2 个表

例如。假设 Table1 有六列(Col1,Col2,Col3,Col4,Col5,Col6) 如果我想将表 1 与表 2 (Col1,Col2,Col3,Col4,Col5,Col7) 连接起来,它应该

否则

Select Table2.col7
where 
first check col1 , col2 and col3 if match found no need go check more 
second check col1 , col2  if match found no need go check more 
third check col1  if match found no need go check more 
last ignore all col1 , col2 and col3 
AND Table1.Col4=Table2.Col4
AND Table1.Col5=Table2.Col5

我的话可能不太清楚,如果有什么问题请大声疾呼

【问题讨论】:

标签: oracle


【解决方案1】:

您不能告诉 SQL 先尝试在某个条件下加入,以防找不到匹配项继续搜索。您可以做的是加入所有允许的组合(在您的情况下匹配 col4 和 col5),然后对匹配进行排名(这样 col1 和 col2 和 col3 上的匹配被认为是最佳等)。然后只保留最佳匹配:

select col7
from
(
  select 
    t1.*, 
    t2.*,
    row_number() over 
    (
      partition by t1.col4, t1.col5
      order by case 
        when t2.col1 = t1.col1 and t2.col2 = t1.col2 and t2.col3 = t1.col3 then 1
        when t2.col1 = t1.col1 and t2.col2 = t1.col2 then 2
        when t2.col1 = t1.col1 then 3
        else 4
    ) as rn
  from table1 t1
  join table2 t2 on t2.col4 = t1.col4 and t2.col5 = t1.col5
)
where rn = 1;

【讨论】:

  • 感谢 Thorsten 和 Vercelli,我正在尝试在代码下方运行,但抛出错误顺便说一句,我现在又介绍了一张表。
  • select DISTINCT CU.ID cu_id, 'XXXX', to_char(RR.EFF_FROM,'DD-MON-YYYY') Active_Date, NVL(to_char(RR.EFF_TO,'DD-MON-YYYY' ), '31-DEC-9999') Inactive_Date from ( Select RL.*, CU.*, RR.*, row_number() over ( partition by RR.Divi_Flag, RR.Brand_Type_Code order by case when RR.Dept_Cd = FLOOR( RL.Sub_Section/10000) 和 RR.Section = FLOOR(MOD(RL.Sub_Section,10000)/100) 和 RR.SubSection = MOD(RL.Sub_Section,100) 然后 1 当 RR.Dept_Cd = FLOOR(RL.Sub_Section/ 10000) 和 RR.Section = FLOOR(MOD(RL.Sub_Section,10000)/100) 然后 2
  • 当 RR.Dept_Cd = FLOOR(RL.Sub_Section/10000) then 3 else 4 ) as rn from TABLE1 RL , TABLE2 CU , TABLE3 RR WHERE RL.Code = CU.FK_Rl_Code AND RL.Brand_Type_Code = RR.Brand_Type_Code 和 NVL(CU.Dividend_Flag, 'X') = NVL(RR.Divi_Flag, 'X') WHERE cu.skeletal_full_ind = 'F' AND cu.discontinued_date IS NULL ) where rn = 1;
  • @user6498752 - 在我看来这是一个新问题。原来的问题得到解答了吗?
  • 不,它是原创的,但听着 thorsten 的建议。如果您有任何其他方法,请告诉我。
【解决方案2】:
Select t2.col7
  from Table1 t1 inner join Table2 t2
    on
  case 
    when t1.col1 = t2.col1 then 1
    when t1.col2 = t2.col2 then 1
    when t1.col3 = t2.col3 then 1
    when t1.Col4=t2.Col4
       and t1.Col5=t2.Col5 then 1
    else 0 end = 1
  ;

【讨论】:

  • @user6498752 我编辑了答案。告诉你这是不是你要找的东西
  • 感谢 Vercelli 抽出宝贵时间,但我这里还有一个问题,选择应基于 t1.col4 和 t1.col5 值,我的意思是如果我将 t1.col4 值设为 (A, B) 和 t1.col5 (1,0),所以我们将有 4 个组合选项 (t1.col4, t1.col5) =(A,1),(A,0),(B,0),( B,1) 对于每个组合,当 t1.col1 = t2.col1 然后 1 当 t1.col2 = t2.col2 然后 1 当 t1.col3 = t2.col3 然后 1 else 0 end = 1 Hope ,你很快就会帮助我...提前非常感谢
  • @vercelli:CASE 表达式不属于WHERE 子句。请改用简单的ANDOR
  • @vercelli:你使用的连接语法在很久以前就已经被冗余了。改用显式连接(INNER JOINCROSS JOIN 等)。
  • @ThorstenKettner - 更改为内部联接。我不喜欢 CASE on WHERE,但我不确定它们是否属于那里。我只是发现它在这种奇怪的情况下很有用。
猜你喜欢
  • 2021-07-28
  • 2021-01-09
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
相关资源
最近更新 更多