如何从表中显示重复项?

时间:2015-01-19 20:29:08

标签: sql database plsql oracle-sqldeveloper plsqldeveloper

我有问题。我尝试从表中显示重复项。我在pl sql中的代码

SELECT intermediary_nr, beneficiary_role, contract_nr
  FROM (SELECT *
          from (select intermediary_nr,
                       beneficiary_role,
                       max(contract_nr) contract_nr
                  from boscs.atcs_commission_beneficiary
                 where beneficiary_role = 'LEAD'
                   and intermediary_nr is not null
                 group by intermediary_nr, beneficiary_role
                 ORDER BY dbms_random.value)

        union all
        SELECT *
          from (select intermediary_nr,
                       beneficiary_role,
                       max(contract_nr) contract_nr
                  from boscs.atcs_commission_beneficiary
                 where beneficiary_role = 'SUP_FOR_LEAD'
                   and intermediary_nr is not null
                 group by intermediary_nr, beneficiary_role
                 ORDER BY dbms_random.value)

        union all
        SELECT *
          from (select intermediary_nr,
                       beneficiary_role,
                       max(contract_nr) contract_nr
                  from boscs.atcs_commission_beneficiary
                 where beneficiary_role = 'COAGENT'
                   and intermediary_nr is not null
                 group by intermediary_nr, beneficiary_role
                 ORDER BY dbms_random.value))

enter image description here

选择intermediary_nr,beneficiary_role,contract_nt if a.contract_nr = b.contract_nr = c.contract_nr

这个关系让我很感兴趣。我想知道如何解决这个问题。有什么想法吗?我想只显示与a,b,c相同的contract_nr信息。其余的没有。有谁知道如何完成这些问题?

1 个答案:

答案 0 :(得分:0)

如果我理解了请求,您只想查看在所有三个结果集中具有相同合同的那些行。如果是这样,没问题。

首先,让我们稍微收紧一下这个问题。你有七个(!)SELECT语句,只需要一个。

select  intermediary_nr, beneficiary_role, max(contract_nr) contract_nr
from    boscs.atcs_commission_beneficiary
where   beneficiary_role in( 'LEAD', 'SUP_FOR_LEAD', 'COAGENT' )
    and intermediary_nr is not null
group by intermediary_nr, beneficiary_role;

我省略了order by条款,因为它似乎没有任何用途。

这将生成如下结果集:

intermediary_nr  beneficiary_role  contract_nr
===============  ================  ===========
            ...  ...               ...
             10  LEAD              100
             10  SUP_FOR_LEAD      100
             20  LEAD              101
             20  SUP_FOR_LEAD      101
             20  COAGENT           101
            ...  ...               ...

对于其余部分,我假设不可能出现如下输出:

intermediary_nr  beneficiary_role  contract_nr
===============  ================  ===========
            ...  ...               ...
             10  LEAD              100
             10  SUP_FOR_LEAD      100
             20  LEAD              101
             20  SUP_FOR_LEAD      101
             20  COAGENT           101
             30  COAGENT           101
            ...  ...               ...

我不知道您的数据是什么样的,所以我不知道这是否是一个有效的假设。只有你能回答这个问题。

现在,您只需要为所有三个角色显示相同合同号的行。在我的示例中,那将是101.如果我们将第一个查询的结果提供给第二个查询,我们可以为该结果执行更多分组:

select   contract_nr
from     <query above>
group by contract_nr
having count(*) = 3;

这将为您提供所需合同的清单。联合那些回到第一个查询,你得到的结果是(我希望)寻找:

with
ContractByRole( intermediary_nr,  beneficiary_role,  contract_nr )as(
  select  intermediary_nr, beneficiary_role, max(contract_nr) contract_nr
  from    boscs.atcs_commission_beneficiary
  where   beneficiary_role in( 'LEAD', 'SUP_FOR_LEAD', 'COAGENT' )
      and intermediary_nr is not null
  group by intermediary_nr, beneficiary_role
),
AllRoles( contract_nr )as(
  select   contract_nr
  from     ContractByRole
  group by contract_nr
  having count(*) = 3
)
select  cbr.*
from    ContractByRole cbr
join    AllRoles       ar
    on  ar.contract_nr = cbr.contract_nr;

生成:

intermediary_nr  beneficiary_role  contract_nr
===============  ================  ===========
             20  LEAD              101
             20  SUP_FOR_LEAD      101
             20  COAGENT           101

如果中间值不符合我显示的模式,则必须稍微调整查询以进行补偿,但这并不困难。