简体   繁体   English

类似aj(asof join)的function如何返回给定日期之前右表中的所有记录?

[英]How to return all records in the right table before the given date with a function similar to aj (asof join)?

In DolphinDB, the asof join returns the latest record before the given time in the right table.在 DolphinDB 中, asof join返回右表中给定时间之前的最新记录。 Is there a function that is similar to aj , but returns all previous records before the given date and forms a new table?是否有类似于aj的 function ,但返回给定日期之前的所有先前记录和 forms 一个新表?

I have the following two tables, tb1 and tb2.我有以下两个表,tb1 和 tb2。 In tb2, each value in the date column is the first day of a month, and each target (A, B and C) has multiple sources.在 tb2 中,日期列中的每个值都是一个月的第一天,每个目标(A、B 和 C)都有多个来源。

My expected process is:我预期的过程是:

  • In the tb2, find the records of which the target value matches S_INFO_WINDCODE of tb1 before the corresponding TRADE_DT date;在tb2中,在对应的TRADE_DT日期之前,找到目标值与tb1的S_INFO_WINDCODE匹配的记录;

  • Join all records with table tb1 to form a new table.将所有记录与表 tb1 连接起来,形成一个新表。

I have simulated some data using the following script:我使用以下脚本模拟了一些数据:

TRADE_DT=sort(take(2021.02.01+3*(1..20),60))
S_INFO_WINDCODE=take(`A`B`C,60)
S_DQ_ADJCLOSE=rand(10.,60)
Ret=rand(1.,60)
tb1=table(TRADE_DT,S_INFO_WINDCODE,S_DQ_ADJCLOSE,Ret)
date=sort(take(date(2021.01M+0..2),3*3*3))
Source=take(`dd`rr`rrd`eee`ggg`yyy`yy`jj`www,3*3*3)
Target=take(`A`A`A`B`B`B`C`C`C,3*3*3)
Weight=rand(10.,3*3*3)
tb2=table(date,Source,Target,Weight)

First, we define a function f to select all records before TRADE_DT from table tb2.首先,我们定义一个function f到select表tb2中TRADE_DT之前的所有记录。 Then generate a new table by joining the results and tb1 with cj (cross join) .然后通过将结果和 tb1 与cj (cross join)连接起来生成一个新表。

def f(tb1,tb2,idate,icode){
        t1=select * from tb1 where TRADE_DT=idate and S_INFO_WINDCODE=icode
        t2=select * from tb2 where date<=idate  and Target=icode context by Target having date=max(date)
        return cj(t1,t2)        
        }

Use the function f with higher-order function peach to process all records in parallel:使用 function f和高阶 function peach并行处理所有记录:

tb=unionAll(peach(f{tb1,tb2,,},tb1.TRADE_DT,tb1.S_INFO_WINDCODE),0)

TRADE_DT   S_INFO_WINDCODE S_DQ_ADJCLOSE     Ret               date       Source Target Weight           
---------- --------------- ----------------- ----------------- ---------- ------ ------ -----------------
2021.02.04 A               4.487512307241559 0.940215857466683 2021.02.01 dd     A      4.061586682219059
2021.02.04 A               4.487512307241559 0.940215857466683 2021.02.01 rr     A      3.668303932063282
2021.02.04 A               4.487512307241559 0.940215857466683 2021.02.01 rrd    A      0.131341586820781
2021.02.04 B               1.054471984971315 0.633344997884706 2021.02.01 eee    B      0.412905525881797
2021.02.04 B               1.054471984971315 0.633344997884706 2021.02.01 ggg    B      0.312636836897582
2021.02.04 B               1.054471984971315 0.633344997884706 2021.02.01 yyy    B      2.967845387756824
2021.02.04 C               6.044307881966234 0.909127941122279 2021.02.01 yy     C      8.218941506929695
2021.02.04 C               6.044307881966234 0.909127941122279 2021.02.01 jj     C      0.563267054967582
2021.02.04 C               6.044307881966234 0.909127941122279 2021.02.01 www    C      8.038489881437271
2021.02.07 A               8.147617720533162 0.746302032610402 2021.02.01 dd     A      4.061586682219059
2021.02.07 A               8.147617720533162 0.746302032610402 2021.02.01 rr     A      3.668303932063282
2021.02.07 A               8.147617720533162 0.746302032610402 2021.02.01 rrd    A      0.131341586820781
2021.02.07 B               0.449538344983012 0.780524794943631 2021.02.01 eee    B      0.412905525881797
2021.02.07 B               0.449538344983012 0.780524794943631 2021.02.01 ggg    B      0.312636836897582
2021.02.07 B               0.449538344983012 0.780524794943631 2021.02.01 yyy    B      2.967845387756824
2021.02.07 C               4.924322243314237 0.217726393137127 2021.02.01 yy     C      8.218941506929695
2021.02.07 C               4.924322243314237 0.217726393137127 2021.02.01 jj     C      0.563267054967582
2021.02.07 C               4.924322243314237 0.217726393137127 2021.02.01 www    C      8.038489881437271
2021.02.10 A               2.242919851560146 0.706920271040872 2021.02.01 dd     A      4.061586682219059
2021.02.10 A               2.242919851560146 0.706920271040872 2021.02.01 rr     A      3.668303932063282
2021.02.10 A               2.242919851560146 0.706920271040872 2021.02.01 rrd    A      0.131341586820781
2021.02.10 B               3.251840437296778 0.908186007523909 2021.02.01 eee    B      0.412905525881797
2021.02.10 B               3.251840437296778 0.908186007523909 2021.02.01 ggg    B      0.312636836897582
2021.02.10 B               3.251840437296778 0.908186007523909 2021.02.01 yyy    B      2.967845387756824
2021.02.10 C               4.774149649310857 0.362120353849605 2021.02.01 yy     C      8.218941506929695
2021.02.10 C               4.774149649310857 0.362120353849605 2021.02.01 jj     C      0.563267054967582
2021.02.10 C               4.774149649310857 0.362120353849605 2021.02.01 www    C      8.038489881437271
2021.02.13 A               9.988255267962813 0.307931573363021 2021.02.01 dd     A      4.061586682219059
2021.02.13 A               9.988255267962813 0.307931573363021 2021.02.01 rr     A      3.668303932063282
2021.02.13 A               9.988255267962813 0.307931573363021 2021.02.01 rrd    A      0.131341586820781

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM