通过忽略那些不再需要的记录来过滤表格

时间:2016-02-20 13:55:47

标签: oracle

我有一张表,包括CONTRACT_ID,ADDENDUM_ID和PAYMENT_MONTH(年度合约的付款日期)。 每次因系统中的任何原因发生更新时:

  1. ADDENDUM_ID udates为+1
  2. PAYMENT_MONTH记录在剩余的几个月内重复
  3. 在附图中,我试图用一个例子(一个包含3个更新的合同)详细解释。 问题是如何编写查询以获取汇总表,忽略重复但不必要的记录(灰色填充的记录),因为附录列上有新的更新。 请注意,原始表中有数百个合约,而示例只包含一个。

    提前致谢。

    enter image description here

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您想要的输出,可能会有所帮助:

with test(contract_id, addendum_id, payment_month) as
(
    select 155, 1, 1 from dual union all
    select 155, 1, 2 from dual union all
    select 155, 1, 3 from dual union all
    select 155, 1, 4 from dual union all
    select 155, 1, 5 from dual union all
    select 155, 1, 6 from dual union all
    select 155, 1, 7 from dual union all
    select 155, 1, 8 from dual union all
    select 155, 1, 9 from dual union all
    select 155, 1, 10 from dual union all
    select 155, 1, 11 from dual union all
    select 155, 1, 12 from dual union all
    select 155, 2, 5 from dual union all
    select 155, 2, 6 from dual union all
    select 155, 2, 7 from dual union all
    select 155, 2, 8 from dual union all
    select 155, 2, 9 from dual union all
    select 155, 2, 10 from dual union all
    select 155, 2, 11 from dual union all
    select 155, 2, 12 from dual union all
    select 155, 3, 10 from dual union all
    select 155, 3, 11 from dual union all
    select 155, 3, 12 from dual       
)
select contract_id, addendum_id, payment_month from (
    select      
           max(addendum_id)  over ( partition by payment_month) as max_addendum_id,
           contract_id, payment_month, addendum_id
    from test
)
where max_addendum_id = addendum_id
order by 1, 2, 3