【问题标题】:Multiple rows in spite of using outer join尽管使用了外连接,但还是多行
【发布时间】:2019-05-30 06:58:32
【问题描述】:

下面有两张桌子

Paam Table
AssignmentID     PersonID         AssignmentType
300000014199240  300000014199145  E
300000014199174  300000014199145  ET

Par Table
ASGResponsID    AssignmentID    PersonID        Responsibility_Type
300000015244074 300000014199240 300000014199145 RC_HR_BP
300000015242351 300000014199240 300000014199145 RC_HR_BP
300000015244070 300000014199240 300000014199145 RC_HR_BP

我想加入这两个表并获得如下输出

PersonID         Responsiblity_Type
300000014199145  RC_HR_BP

我正在使用以下查询

select 
par.PersonID, par.Responsibility_Type 
from 
per_all_assignments_m paam, per_asg_responsibilities par
where
sysdate between nvl(paam.effective_start_date,sysdate) and 
nvl(paam.effective_end_date,sysdate)
and  paam.assignment_type='E'
and paam.assignment_id = par.assignment_id(+)
and paam.person_id = '300000014199145';

相反,我得到如下输出

PersonID         Responsiblity_Type
300000014199145  RC_HR_BP
300000014199145  RC_HR_BP
300000014199145  RC_HR_BP

尽管使用了左外连接,但我得到了多行,为什么会这样?

有人可以帮我理解吗?

谢谢, 希瓦姆

【问题讨论】:

  • 哇,我已经有一段时间没有看到这种古老的外连接语法了。您使用的是哪个 Oracle 版本?
  • 我使用的是 Oracle 11
  • 而任务究竟是什么?显示类型为“E”的人员“300000014199145”分配的所有不同职责类型?
  • @shivam:然后使用正确的 ANSI 连接 (from a left outer join b on ...)。您使用的连接语法与标准 SQL 连接变得多余。
  • 我只想在输出中显示personId和responsibility_type,因为这个特定的人只有一个response_type,那么输出中应该只有1行。

标签: sql oracle outer-join multiple-records


【解决方案1】:

首先使用具有正确语法的常规join

select par.PersonID, par.Responsibility_Type 
from per_all_assignments_m paam join
     per_asg_responsibilities par
     on paam.assignment_id = par.assignment_id and
        paam.person_id = par.person_id  -- presumably also needed for the join
where sysdate between nvl(paam.effective_start_date, sysdate) and 
nvl(paam.effective_end_date, sysdate) and
      paam.assignment_type = 'E' and
      paam.person_id = '300000014199145';

您对外部连接的意图可能是:

select par.PersonID, par.Responsibility_Type 
from per_asg_responsibilities par left join
     per_all_assignments_m paam
     on paam.assignment_id = par.assignment_id and
        paam.person_id = par.person_id  and
        sysdate between nvl(paam.effective_start_date, sysdate) and 
nvl(paam.effective_end_date, sysdate) and
        paam.assignment_type = 'E' and
where par.person_id = '300000014199145';

【讨论】:

  • 您好,感谢您的回复。我已经尝试过这些,但仍然得到相同的多行。我想我可能需要使用 row_num 函数
【解决方案2】:

根据您的数据,您得到了正确的结果。 par 表中的所有三行都具有相同的 assignmentId (300000014199240),因此无论连接语法如何,您的查询都会返回。

添加“Distinct”子句会得到你想要的结果。

select DISTINCT
  par.PersonID, par.Responsibility_Type 
from 
  per_all_assignments_m paam, per_asg_responsibilities par
where
  sysdate between nvl(paam.effective_start_date,sysdate) and 
  nvl(paam.effective_end_date,sysdate)
  and  paam.assignment_type='E'
  and paam.assignment_id = par.assignment_id(+)
  and paam.person_id = '300000014199145';

我在http://sqlfiddle.com/#!4/d7b7f/8 的 SQL Fiddle 中进行了设置。注意:我删除了日期字段引用,因为您没有向我们提供这些数据,但根据您描述问题的方式,它不应该影响结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-13
    • 2018-12-04
    • 1970-01-01
    • 2013-08-11
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多