【问题标题】:map values based on condition in kdb根据 kdb 中的条件映射值
【发布时间】:2021-07-01 02:45:51
【问题描述】:

我有一组如下的交易数据:

date sym asset points tenor
2020.11.10 ABC FX 0.2 "AN"
2020.11.15 ABB DX 0.1 "AN"
2020.11.17 AAA FX 0.1 "ON"
2020.11.1 ABB FX 0.3 "AN"
2020.11.1 AAA FX 0.1 "SW"

对于asset=FX,期限必须映射/更新如下:

"AN"->"AN/3S"

"ON"->"0N"

"SW"->"1W"  

结果应该是这样的:

date sym asset point tenor
2020.11.10 ABC FX 0.2 "AN/3s"
2020.11.15 ABB DX 0.1 "AN"
2020.11.17 AAA FX 0.1 "0N"
2020.11.1 ABB FX 0.3 "AN/3s"
2020.11.1 AAA FX 0.1 "1W"

我已经尝试过幼稚的方法。有没有办法以编程方式做到这一点? 作为参考,表名是数据,它是按日期分区的

【问题讨论】:

    标签: kdb


    【解决方案1】:

    试试下面的查询,t 是表:

    update tenor: (`AN`ON`SW!`$("AN/3S";"0N";"1W"))@/:tenor from t
    

    它在字典`AN`ON`SW!`$("AN/3S";"0N";"1W") 中查找each tenor on the right。

    【讨论】:

    • update tenor: (`AN`ON`SW!`$("AN/3S";"0N";"1W"))@/:tenor from t 在我的情况下,男高音是一个字符串,资产是符号,即FX. i want to update as table rather than dictionary and store the same in data like ```data:update tenor: (ANONSW!$("AN/3S";"0N";"1W"))@/:tenor from t``` par: Unsupported operation on a partitioned table or component thereof kdb I'm getting this the error. FYI: All the tenor values are strings and FX is symbol. data: update tenor^($"AN/S3";0N;1W) AN ONSW from (select tenor from data where asset = FX) 抛出不匹配类型错误我该如何解决这个错误。
    【解决方案2】:

    我们可以使用 find (?) 运算符为要更新的期限创建一组索引。 然后我们使用这些索引来索引我们相应的地图。

    最后,我们用tenor 填写,以防我们的映射中没有涵盖任何男高音(您可以用您选择的合适的填写替换它)。

    where 子句确保我们只对外汇资产执行此操作。由于您似乎正在使用日期分区表,因此我还添加了一个日期子句以进一步限制查询

    q)update ("AN/3S";"0N";"1W") ("AN";"ON";"SW")?tenor from select from data where date within 2020.11.01 2020.11.20,asset = `FX
    date       sym asset point tenor
    ----------------------------------
    2020.11.10 ABC FX    0.2   "AN/3S"
    2020.11.17 AAA FX    0.1   "0N"
    2020.11.01 ABB FX    0.3   "AN/3S"
    2020.11.01 AAA FX    0.1   "1W"
    

    【讨论】:

    • par: 对分区表或其组件 kdb 不支持的操作 我收到此错误。仅供参考:所有的中值都是字符串,FX 是符号。 data: update tenor^(`$"AN/S3";`0N;`1W) `AN`ON`SW from (select tenor from data where asset = `FX) 抛出 mismatchedtypes 错误或类似 update tenor^("AN/S3";"0N";"1W") "AN""ON""SW" from (select tenor from data where asset = `FX) 给出错误消息不兼容 lengeh
    • 您不能直接对分区数据执行更新语句,这可以解释您的 par (`partition) 错误。您需要首先将数据选择到内存中(您在第二个示例中似乎正在这样做),然后将更新应用于该数据。警告 - 如果您在大型日期分区数据库上操作,您还应该在您的选择中包含一个日期子句(选择...从...其中日期在(d1;d2)内)这会将您的查询限制为您的日期'实际上对(并可能加快速度)感兴趣。
    • 我已更新答案以反映新信息
    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2021-12-08
    • 2019-11-02
    • 2017-01-27
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多