【问题标题】:Match and compare data from two tables匹配和比较两个表中的数据
【发布时间】:2017-09-30 19:40:29
【问题描述】:


我在匹配来自 2 个不同表的数据时遇到问题。

table1: a,b,c,d,e (col)
table2: a,d,e,f,g (col)

如何将table1 col a,d,e中的数据与table2 col a,d,e进行匹配 如果 table1 中的行与 table2 中的行匹配然后停止循环?

在我的脚本中,结果总是在匹配数据时重复(当 table1 中的数据匹配时,它的循环仍然没有与 table2 中的其他数据锁定)。

select distinct x.a, y.a, x.d, y.d, x.e, y.e
from table1 x,
     table2 y
where x.a = y.a(+) and x.d = y.d(+) and x.e = y.e(+)


数据样本...

table1

col a--b--c--d--e
'Ryan'--'Sofia'--'Bulgaria'--'January'--'107'
'Dony'--'Vienna'--'Austria'--'March'--'103'
'Ryan'--'Berlin'--'Germany'--'January'--'107'
'Dony'--'Milan'--'Italy'--'March'--'103'

table2

col a--d--e--f--g
'Ryan'--'January'--'107'--'Travel'--'5'
'Ryan'--'January'--'107'--'Bussiness'--'4'
'Dony'--'March'--'103'--'Bussiness'--'9'
'Dony'--'March'--'103'--'Bussiness'--'3'



查询

select distinct x.a, y.a, x.d, y.d, x.e, y.e
from table1 x,
     table2 y
where x.a = y.a(+) and x.d = y.d(+) and x.e = y.e(+)


结果是
table1 1st_row 与 table2 1st_row 匹配
table1 2nd_row 与 table2 3rd_row 匹配
table1 3rd_row 与 table2 1st_row 匹配(匹配重复)
table1 4th_row 与 table2 3rd_row 匹配(匹配重复)


但需要的结果是
table1 1st_row 与 table2 1st_row 匹配
table1 2nd_row 与 table2 3rd_row 匹配
table1 3rd_row 与 table2 2nd_row 匹配
table1 4th_row 与 table2 4th_row 匹配

我不明白如何使用程序或案例
请帮助解决这个问题...谢谢

【问题讨论】:

  • 请修正您的格式,告诉我们您正在使用什么数据库,并使用(+) 摆脱那种陈旧的连接语法。
  • 对不起蒂姆,我一周前刚刚学习 oracle 数据库,我不太了解你对古连接语法的意思.. 抱歉
  • 哦,thanx kinchit dalwani
  • 您当前的连接条件不区分 table2 中的第 1,2 和 3,4 行。您必须添加逻辑才能以您想要的方式加入。

标签: sql oracle


【解决方案1】:

为组中的行编号并连接具有相同编号的行。

select *
  from ( select T1.*, row_number() over(partition by a,d,e order by NULL) num
           from Table1 T1
       ) x
  left join (
         select T2.*, row_number() over(partition by a,d,e order by NULL) num
           from Table2 T2
       ) y
    on x.a = y.a and x.d = y.d and x.e = y.e and x.num=y.num

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2013-04-02
    • 2012-09-05
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多