Chenrui Su
Chenrui Su

Reputation: 155

Pass multiple function parameter by column kdb q

I'm learning kdb\q recently. I have a function declared as below:

func_demo:{[id;time] select last synp from synp_test where instrument_id = id, tp_time < time}

And the function works perfectly. For example, func_demo[1;13:00:08] will give me a single line output:

synp
----
7094.157

However, I want to pass multiple rows of data into the function, and the function gives me multiple lines of result.

For example, I have table t looks like the following:

id     time
--------------
1     13:00:04
2     13:00:16
...

If possible, I want to pass in the entire id column and time column, and my expected result would be like:

id   time        synp
-------------------------
1    13:00:04    7094.157
2    13:00:08    8085.867
...

Is there a way can solve this issue? Thanks so much!

Upvotes: 0

Views: 660

Answers (1)

nyi
nyi

Reputation: 3229

You can use the aj it will return the prevailing prices if there is no exact match for time.

q)synp_test:update `g#id from `tp_time xasc  ([] tp_time:13:00:04 13:00:08 13:00:12  ; id: 1 2 3 ;price:10 11 12)   //
q)t1:([] id: 1 2; tp_time:13:00:04 13:00:09 )
aj[`id`tp_time;t1;synp_test]
id  tp_time     price
1   13:00:04    10
2   13:00:09    11

lj can be used in cases when we want the exact matches

Upvotes: 1

Related Questions