下面是源数据
Due Date Status Activity Name Completed_date
Completed Maintenance (ADV) 23-Feb-23
9-Apr-23 Assessment Maintenance (ADV)
Completed Records Management Standards 10-Mar-23
16-Apr-23 Pending Records Management Standards
16-Apr-23 Assessment Records Management Standards
10-Mar-23 Scheduled Records Management Standards
Completed Monitor Compliance 14-Feb-23
9-Apr-23 Assessment Monitor Compliance
Completed Monitor Customer 23-Feb-23
9-Apr-23 Pending Monitor Customer
我需要的是
Due Date Status Activity Name Completed_date
9-Apr-23 Completed Maintenance (ADV) 23-Feb-23
16-Apr-23 Completed Records Management Standards 10-Mar-23
9-Apr-23 Completed Monitor Compliance 14-Feb-23
9-Apr-23 Completed Monitor Customer 23-Feb-23
我写了两个查询,一个是获取最大的截止日期,完成日期按活动名称分组
select MAX(DUE_DATE) as due_date, ACTIVITY_NAME, MAX(COMPLETED_DATE) as completed_date from activity_history group by ARCM_ACTIVITY_NAME
还有一个用于状态列的自定义排序
select t.*
from (select R.*,
row_number() over (partition by ACTIVITY_NAME
order by (case when status = 'Completed' then 1
when status = 'Scheduled' then 3
else 2
end)) as rank
from activity_history R
) t
where rank = 1;
我如何将其合并到单个查询中以获得上述所需的输出?
2条答案
按热度按时间vd8tlhqk1#
您可以聚合和使用
MAX(...) KEEP (DENSE_RANK FIRST...)
:其中,对于样本数据:
输出:
| 到期日|状态|活动名称|完成日期|
| --------------|--------------|--------------|--------------|
| 2019 -04-09 00:00:00|已完成|维护(ADV)|2019 -02-23 00:00:00|
| 2019 -04-09 00:00:00|已完成|监查员合规性|2019 -02-14 00:00:00|
| 2019 -04-09 00:00:00|已完成|监控客户|2019 -02-23 00:00:00|
| 2019 -04-16 00:00:00|已完成|档案管理标准|2019 -04-10 00:00:00|
fiddle
3xiyfsfu2#
使用
max
函数(对于due_date
)的解析形式。样本数据:
查询(检查第3行):