如何编写自定义Kusto函数实现标的投资工具查询?
Kusto 标的映射函数修正方案
原代码存在的问题
- 未使用传入的
SecId参数过滤数据,导致返回结果无法匹配目标证券 - 列名错误:规则要求返回
UnderlyingInvestment,但代码中误写为UnderlyingInv - 获取最新数据的方式效率低下,且未保证单条匹配
- 返回逻辑未正确提取目标标的值
修正后的函数代码
.create-or-alter function Risk_Ticker(SecId: string) { database('test').ETLN_SecMaster // 过滤目标证券的记录 | where Code == SecId // 获取该证券的最新版本数据(按FileDateTime取最大值) | summarize arg_max(FileDateTime, *) by Code // 根据RecordClass规则映射标的 | extend MappedUnderlying = case( RecordClass == "SwapInvestment", InvestmentIn, RecordClass in ("Warrant", "Option", "Future"), UnderlyingInvestment, Code // 其他类型直接返回自身Code ) // 提取并返回标的值 | project MappedUnderlying | toscalar() }
关键修改说明
- 添加目标过滤:通过
where Code == SecId确保只处理传入的目标证券数据 - 优化最新数据获取:用
arg_max(FileDateTime, *) by Code替代原有的两次查询,性能更优且能直接拿到单条最新记录 - 修正列名错误:将
UnderlyingInv改为规则要求的UnderlyingInvestment - 简化返回逻辑:先完成过滤和映射,再通过
toscalar()直接返回单个标的值
内容的提问来源于stack exchange,提问作者A_4964835789
相关产品推荐
相关产品推荐

