仅返回列值已更改的行

时间:2016-12-12 09:24:21

标签: sql

我正在尝试以下查询,但返回67行。 但是我只想在" Is实体是PEP"价值已经改变。

select tpe.extrnl_id_val as "CE",
CASE WHEN tc.POLICALLY_EXPOSED_PERSON_IND=1 THEN (select 'Yes' from dual)
ELSE (select 'No' from dual) END as "Is this entity a PEP",
tc.upd_ts as "Date"
from rkyc_em_01.tclient_jrnl tc,rkyc_em_01.tparty_extrnl_id tpe
where tc.party_id=tpe.party_id
and extrnl_id_val in ('1231554')
group by POLICALLY_EXPOSED_PERSON_IND, extrnl_id_val,tc.upd_ts
order by tc.upd_ts desc

输出:

CE        PEP   Date
1231554   Yes   10/20/2016 11:52:36 AM
1231554   Yes   10/20/2016 11:52:24 AM
1231554   Yes   10/20/2016 11:50:11 AM
1231554   Yes   10/14/2016 10:38:30 AM
1231554   No    8/18/2016 10:35:51 AM
1231554   No    7/25/2016 3:22:05 AM
1231554   No    2/17/2016 1:58:40 AM
1231554   No    10/9/2015 3:23:54 AM
1231554   No    6/26/2015 10:33:02 AM
1231554   No    6/16/2015 7:43:23 AM
1231554   No    6/27/2014 4:51:19 PM
1231554   No    2/25/2014 9:42:21 AM
1231554   No    2/18/2014 9:27:30 AM
1231554   No    12/12/2013 9:29:10 AM
1231554   No    11/25/2013 6:56:23 AM
1231554   No    5/31/2013 7:37:32 AM
1231554   No    5/31/2013 7:37:27 AM
1231554   No    3/27/2013 8:09:45 PM
1231554   No    3/4/2013 5:33:02 PM
1231554   No    12/10/2012 7:46:33 PM
1231554   No    9/24/2012 8:47:30 PM
1231554   No    9/24/2012 8:47:22 PM
1231554   No    11/21/2011 11:32:28 AM
1231554   No    11/21/2011 11:32:27 AM
1231554   No    11/18/2011 3:48:04 PM
1231554   No    8/6/2011 1:10:14 PM
1231554   No    8/6/2011 7:09:18 AM
1231554   No    8/6/2011 6:46:55 AM

预期产出:

CE        PEP   Date
1231554   Yes   10/14/2016 10:38:30 AM
1231554   No    8/6/2011 6:46:55 AM

2 个答案:

答案 0 :(得分:0)

这应该让你去。

    SELECT * FROM (SELECT tpe.extrnl_id_val as "CE",
    CASE WHEN tc.POLICALLY_EXPOSED_PERSON_IND=1 THEN 
     (select 'Yes' from dual)
    ELSE 
     (select 'No' from dual) END as "Is this entity a PEP",
    tc.upd_ts AS "Date"
    from rkyc_em_01.tclient_jrnl tc, rkyc_em_01.tparty_extrnl_id tpe
    where tc.party_id = tpe.party_id
    and extrnl_id_val in ('1231554')
    group by POLICALLY_EXPOSED_PERSON_IND, extrnl_id_val,tc.upd_ts
    order by tc.upd_ts desc) T
  WHERE --add your condition here.

答案 1 :(得分:0)

以下是在标准SQL中执行此操作的方法。您使用LAG查看上一条记录,看看是否有更改。

select 
  extrnl_id_val as "ce",
  case when current_value = 1 then 'yes' else 'no' end as "is this entity a pep",
  upd_ts as "date"
from
(
  select 
    tpe.extrnl_id_val,
    tc.upd_ts,
    tc.polically_exposed_person_ind as current_value,
    lag(tc.polically_exposed_person_ind) over (partition by tpe.extrnl_id_val 
                                               order by tc.upd_ts desc) as previous_value
  from rkyc_em_01.tclient_jrnl tc
  join rkyc_em_01.tparty_extrnl_id tpe using(party_id)
  where tpe.extrnl_id_val in ('1231554')
)
where current_value <> previous_value or previous_value is null
order by upd_ts desc;