【问题标题】:How to build logic to find the history of Red and Amber records with below example如何通过以下示例构建逻辑以查找红色和琥珀色记录的历史记录
【发布时间】:2021-05-18 18:36:04
【问题描述】:
with archer_summary_base as (select * from kri.archer_kri_latest_summary 
where segment='All Segments'  and  Level_I_Risk!='' and metric_identifier in ('Mandatory') and  (metric_results_status='Amber' or metric_results_status='Red' or metric_results_status is null) and
(month='January' or month='February' or month='March' or month='December' or month='November' or  month='April'  or month is null) AND (year=2021 or year=2020 or year is null) 
and status='Active' and originated_source ='Archer'),
--select * from archer_summary_base
archer_summary_main as (select * from kri.archer_kri_latest_summary where metric_id in (select  metric_id from archer_summary_base)
and (month='January' or month='February' or month='March' or month='December' or month='November' or  month='April'  or month is null)
AND (year=2021 or year=2020 or year is null)  and segment='All Segments' and Level_I_Risk !='' and  originated_source ='Archer' and
metric_identifier in ('Mandatory')),
breach_kri_report AS (select metric_id,
trim(Level_I_Risk) as Level_I_Risk,
trim(Metric_Name) as Metric_Name,
(case when format in ('Percentage') then concat(regexp_replace(regexp_replace(Green_threshold_min,'\\.0+$',''),'(\\d+\\.\\d+?)0+$','$1'),'%','-',regexp_replace(regexp_replace(Green_threshold_max,'\\.0+$',''),'(\\d+\\.\\d+?)0+$','$1'),'%')
when format in ('Monetary Amount') then concat('$',format_number(cast(Green_threshold_min as bigint),0),' -','$',format_number(cast(Green_threshold_max as bigint),0))
when format in ('Number') then concat(cast(Green_threshold_min as bigint),'-',cast(Green_threshold_max as bigint)) 
end) as Green_Threshold,
trim(PATH_TO_GREEN_INDICATOR) as P2G_Indicator,
concat(substring(Month,1,3), '-', substring(year,3,4)) as Month_Name,
(case when format in ('Percentage') then concat(cast(metric_value as bigint),'%')
when format in ('Monetary Amount') then  concat('$',(format_number(cast(metric_value as bigint),0)))
when format in ('Number') then format_number(cast(metric_value as bigint),0) end) as DerivedValue_Avg
from archer_summary_main group by metric_id,Level_I_Risk,Metric_Name,Green_threshold_min,Green_threshold_max,PATH_TO_GREEN_INDICATOR,month,year,format,metric_value),
--Select * from breach_kri_report,
exsummary as (SELECT b.Level_I_Risk,
b.Metric_Name,
b.Green_Threshold,
b.P2G_Indicator,
case when Month_Name='Nov-20' then DerivedValue_Avg  end as Nov_20,
case when Month_Name='Dec-20' then DerivedValue_Avg  end as Dec_20,
case when Month_Name='Jan-21' then DerivedValue_Avg  end as Jan_21,
case when Month_Name='Feb-21' then DerivedValue_Avg  end as Feb_21,
case when Month_Name='Mar-21' then DerivedValue_Avg  end as Mar_21,
case when Month_Name='Apr-21' then DerivedValue_Avg  end as Apr_21
FROM breach_kri_report b)
select  
t.Level_I_Risk,
t.Metric_Name,
t.Green_Threshold,
t.P2G_Indicator,
collect_list(Nov_20)[0] as Nov_20,
collect_list(Dec_20)[0] as Dec_20,
collect_list(Jan_21)[0] as Jan_21,
collect_list(Feb_21)[0] as Feb_21,
collect_list(Mar_21)[0] as Mar_21,
collect_list(Apr_21)[0] as Apr_21
from exsummary t
group by t.Level_I_Risk, t.Metric_Name, t.Green_Threshold, t.P2G_Indicator

以下结果显示在屏幕截图中

当我运行查询时,被圈出的“绿色”不应该出现,因为我想获取红色和琥珀色的历史记录。我可以将绿色记录作为历史的一部分,但不是在开始时或最近几个月内

以下是我想要实现的目标

【问题讨论】:

  • table_A 中有什么?您包含的查询似乎也不完整
  • @umberto-petrov 我已经完成了查询

标签: sql hive logic


【解决方案1】:

这是一个可能的查询的大纲:

使用窗口函数发出子查询,按年月分区,按年、月、pk_key升序排序选择最低的pk_key,并为每个分区选择第一个结果。 然后将结果与where 子句和第二个约束结合使用:

... where not(pk_key in (first_query.pk_key) and metric_results_status = 'GREEN')

抱歉,我无法提供更具体的信息,因为您提供的详细信息并不清楚您的数据库结构实际上是什么样的。

愿上帝怜悯你的灵魂!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多