仅在最大日期而不是在组的其他条目中显示具有特定值的组
Posted
技术标签:
【中文标题】仅在最大日期而不是在组的其他条目中显示具有特定值的组【英文标题】:Display group having certain value only on max date and not in other entries of group 【发布时间】:2021-06-09 05:02:17 【问题描述】:我希望显示在 id 列上分组的组,其中 trans_cd='Audit'
仅在该组的 max trans_proc_dt 上。 max trans_proc_dt 不应该有任何其他trans_cd='Audit'
execpt。
ID TRANS_PROC_DT TRANS_CD TRANS_AMT
165 5/13/2020 Renewal 553
165 10/22/2020 Cancellation -376
165 11/24/2020 Audit 3
165 6/2/2021 Change 0
165 6/2/2021 Audit -7
165 6/3/2021 Audit 0
497 5/1/2020 Renewal 1394
497 1/11/2021 Cancellation -578
497 2/10/2021 Audit -3
497 4/28/2021 Audit 76
497 5/12/2021 Audit -73
497 6/2/2021 Change 0
511 4/27/2020 Renewal 4409
511 7/30/2020 Change 0
511 10/5/2020 Cancellation -2558
511 2/18/2021 Audit 2806
577 5/15/2020 Renewal 829
577 2/12/2021 Audit -123
577 4/28/2021 Audit 118
577 5/12/2021 Audit 5
577 6/2/2021 Change 0
577 6/2/2021 Audit -5
577 6/3/2021 Audit 0
577 12/4/2020 Renewal 1996
577 6/2/2021 Change 0
751 5/13/2020 Renewal 1307
751 1/28/2021 Cancellation -523
751 3/3/2021 Audit 481
751 4/28/2021 Audit 120
751 5/12/2021 Audit -601
751 6/2/2021 Change 0
751 6/2/2021 Audit 601
751 6/3/2021 Audit 0
984 5/13/2020 Renewal 1081
984 11/2/2020 Change 0
984 6/3/2021 Audit 0
我的输出应该是
ID TRANS_PROC_DT TRANS_CD TRANS_AMT
511 4/27/2020 Renewal 4409
511 7/30/2020 Change 0
511 10/5/2020 Cancellation -2558
511 1/27/2021 Renewal 4409
511 2/18/2021 Audit 2806
984 5/13/2020 Renewal 1081
984 11/2/2020 Change 0
984 6/3/2021 Audit 0
我想不出如何获取我的结果集。
【问题讨论】:
我不太遵循逻辑来获得您显示的结果。你能详细说明一下吗? 【参考方案1】:您可以使用分析函数计算每组Audit
代码的绝对最大日期和最小日期,然后将它们进行比较:如果其他日期没有更多此类事务发生,则它们应该相等。
with a(ID, TRANS_PROC_DT, TRANS_CD, TRANS_AMT) as ( select 165, '5/13/2020', 'Renewal', 553 from dual union all select 165, '10/22/2020', 'Cancellation', -376 from dual union all select 165, '11/24/2020', 'Audit', 3 from dual union all select 165, '6/2/2021', 'Change', 0 from dual union all select 165, '6/2/2021', 'Audit', -7 from dual union all select 165, '6/3/2021', 'Audit', 0 from dual union all select 497, '5/1/2020', 'Renewal', 1394 from dual union all select 497, '1/11/2021', 'Cancellation', -578 from dual union all select 497, '2/10/2021', 'Audit', -3 from dual union all select 497, '4/28/2021', 'Audit', 76 from dual union all select 497, '5/12/2021', 'Audit', -73 from dual union all select 497, '6/2/2021', 'Change', 0 from dual union all select 511, '4/27/2020', 'Renewal', 4409 from dual union all select 511, '7/30/2020', 'Change', 0 from dual union all select 511, '10/5/2020', 'Cancellation', -2558 from dual union all select 511, '2/18/2021', 'Audit', 2806 from dual union all select 577, '5/15/2020', 'Renewal', 829 from dual union all select 577, '2/12/2021', 'Audit', -123 from dual union all select 577, '4/28/2021', 'Audit', 118 from dual union all select 577, '5/12/2021', 'Audit', 5 from dual union all select 577, '6/2/2021', 'Change', 0 from dual union all select 577, '6/2/2021', 'Audit', -5 from dual union all select 577, '6/3/2021', 'Audit', 0 from dual union all select 577, '12/4/2020', 'Renewal', 1996 from dual union all select 577, '6/2/2021', 'Change', 0 from dual union all select 751, '5/13/2020', 'Renewal', 1307 from dual union all select 751, '1/28/2021', 'Cancellation', -523 from dual union all select 751, '3/3/2021', 'Audit', 481 from dual union all select 751, '4/28/2021', 'Audit', 120 from dual union all select 751, '5/12/2021', 'Audit', -601 from dual union all select 751, '6/2/2021', 'Change', 0 from dual union all select 751, '6/2/2021', 'Audit', 601 from dual union all select 751, '6/3/2021', 'Audit', 0 from dual union all select 984, '5/13/2020', 'Renewal', 1081 from dual union all select 984, '11/2/2020', 'Change', 0 from dual union all select 984, '6/3/2021', 'Audit', 0 from dual ) , last_dt as ( select a.* , min( case trans_cd when 'Audit' then to_date(TRANS_PROC_DT, 'mm/dd/yyyy') end ) over(partition by id) as audit_dt , max(to_date(TRANS_PROC_DT, 'mm/dd/yyyy')) over(partition by id) as max_dt from a ) select id , trans_proc_dt , trans_cd , trans_amt from last_dt where max_dt = audit_dt order by id, to_date(TRANS_PROC_DT, 'mm/dd/yyyy')
身份证 | TRANS_PROC_DT | TRANS_CD | TRANS_AMT --: | :------------ | :----------- | --------: 511 | 2020 年 4 月 27 日 |更新 | 4409 511 | 2020 年 7 月 30 日 |改变 | 0 511 | 2020 年 10 月 5 日 |取消 | -2558 511 | 2021 年 2 月 18 日 |审计 | 2806 第984章2020 年 5 月 13 日 |更新 | 1081 第984章2020 年 11 月 2 日 |改变 | 0 第984章2021 年 6 月 3 日 |审计 | 0
db小提琴here
【讨论】:
【参考方案2】:你可以试试下面-
Select
*
from
tableName tn
where
ID not in
(
Select
ID
from
tableName t
where
trans_cd = 'Audit'
and TRANS_PROC_DT <> (
Select
max(TRANS_PROC_DT)
from
tableName t1
where
t1.ID = t.ID)
)
【讨论】:
以上是关于仅在最大日期而不是在组的其他条目中显示具有特定值的组的主要内容,如果未能解决你的问题,请参考以下文章