一、Hive开窗函数根据特定条件取上一条最接近时间的数据(单个开窗函数,实际取两个窗口)
针对于就诊业务,一次就诊,多个处方,处方结算时间可能不一致,然后会有多个AI助手推荐用药,会多有多条推荐日志,且推荐日志时间和处方结算时间不一致,日志仅能关联到就诊级别的粒度,需要找到处方结算前一条的推荐记录,因此针对一次就诊开窗函数,只能开一个时间窗口,但是有可能有两个处方,所以需要找到两个处方前一条推荐。所以需要根据hive的窗口函数再加条件,实现一个窗口,筛选两条数据出来。
select
t1.*
,case when substring(t1.gmt_created,1,19)=substring(t1.gmt_created_max,1,19) then 1 else 0 end as use_flagfrom (
select
t1.*
,max(casewhen t1.log_type='2-2' and substring(t1.gmt_created, 1, 19) <= substring(t4.expense_date, 1, 19) then substring(t1.gmt_created, 1, 19)end) over(partition by t1.visitCode, t4.expense_date) as gmt_created_max
from wedw_dw.unfold_chdisease_gpt_opt_log_df t1
left join (select visit_no
,mi_card_no
,expense_date
from wedw_dw.doris_yyf_styy_txynhis_record_settle_bill_detail_df_tmp
group by visit_no
,mi_card_no
,expense_date
) t2 on t1.visitCode=t2.visit_no and t1.patientIdNo=t2.mi_card_no
) t1