根据行集之间的变化过滤行

时间:2014-12-09 15:38:30

标签: sql oracle group-by aggregate-functions

我有以下示例数据:

LOCAN_ACCOUNT LOAN_VERSION LENDER PROPORTION PARAM_1
------------- ------------ ------ ---------- -------
            1            1 A             0.6 a      
            1            1 B             0.4 b      
            1            2 A             0.6 a      
            1            2 B             0.4 b      
            1            3 A             0.6 a      
            1            3 B             0.4 b      
...
...
            2            1 A            0.55 a      
            2            1 B            0.45 b      
            2            2 A            0.55 a      
            2            2 B            0.45 b      
            2            2 C               0 c      -- << Note the addition of lender C in LOAN_VERSION = 2
            2            3 A            0.55 a      
            2            3 B            0.45 b      
...
...
            3            1 A           0.555 a      
            3            1 B           0.445 b      
            3            2 A           0.555 a      
            3            2 B           0.445 bbbbb  -- << Note the modification of PARAM_1 for lender B in LOAN_VERSION = 2
...
...
            4            1 A           0.555 a      
            4            1 B           0.445 b      
            4            2 A           0.555 a      
            4            2 D           0.445 d      -- << Note the modification of lenders from B to D in loan versions 1 and 2

要求:

预期输出为:

LOAN_ACCOUNT SHOULD_BE_RETURNED
------------ ------------------
           1 Yes                
           2 No                
           4 No                
           3 No                

当列SHOULD_BE_RETURNED'Yes'时,对于贷款帐户,对于其所有贷款版本,贷方,其比例和param_1列都没有变化。上述示例中的贷款帐户1满足这些条件。

当 -

时,列应为"No"
  1. 任何版本的贷款都有新的贷方。贷款帐户"C"中增加了贷方2。因此,对于贷款帐户2SHOULD_BE_RETURNED = "No"

  2. 在所有贷款版本中,任何贷方的比例或param_1值都没有变化。请注意贷款帐户param_1的{​​{1}}更改。因此也是3

  3. 贷方应该没有变化。请注意贷方从SHOULD_BE_RETURNED = "No""B"对贷款帐户"D"的更改。因此也是4

  4. 我尝试了什么:

    到目前为止,我只管理了这个,但它也给了我错误的输出:

    SHOULD_BE_RETURNED = "No"

    对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

一种方法是将每个版本的值聚合在一起,然后检查它们是否相同:

select loan_account,
       (case when min(lpp) = max(lpp) then 'Y' else 'N' end) as should_be_returned
from (select loan_account, loan_version,
             list_agg(lender || '-' || proportion || '-' || param_1, ', ') within group (order by lender, proportion, param_1) as lpp
      from sampledata
      group by loan_account
     )
group by loan_account