调用具有多个值的函数 kdb\q
call function with multiple value kdb\q
我有一个函数声明:
func:{[id;time] select last synp from synp_t where instr_id = id, tp_time < time}
其中 instr_id
的类型为 i
,tp_time
的类型为 v
。
例如,func[8;05:00:11]
工作正常并给我值 17.55
。
但是,如果我尝试 func[8 1;05:00:11 07:10:59]
,我得到:
'length ERROR: incompatible lengths (different lengths of operands for synchronized operation or table columns lengths are not the same
我想得到的是,比如17.55 9.66
.
如果我执行 select res:func_demo[id;time]from tab
,也会弹出同样的错误,其中 tab
是 table,有两列 instr_id
和 tp_time
。
我想 enlist
可能会解决问题,但我不知道如何使用它。我该如何解决这个问题?
func'[8 1;05:00:11 07:10:59]
应该可以
它使用 each both 副词,它将在两个输入列表的相应元素之间应用函数
可以在此处找到有关副词的更多信息http://code.kx.com/q/ref/adverbs/。他们很强大。
许多内置函数都能够将原子和列表作为输入并相应地工作,但是对于用户定义的函数,您很可能需要利用副词在各种情况下使用它们
我建议使用 aj
来获取结果,查看您的查询,您似乎想要给定 id
的最后价格并且小于输入 time
.
q)func:{[id;time] aj[`instr_id`tp_time; ([] instr_id:id;tp_time: time);synp_t]}
q)func[1 1 2;05:00:11 07:10:59 10:10:00]
与 each-both
你正在调用函数 n 次并选择数据 n 次。
aj
是 Kdb 提供的强大功能之一 asof
价格。
其实不需要func
函数,直接用aj
即可得到结果。
aj[`instr_id`tp_time; update instr_id:id, tp_time: time from tab;synp_t]
这里的两个答案都很好,但没有详细说明为什么会出现 'length
错误。错误全归咎于您的 where
子句 where instr_id = id, tp_time < time
。当您传递一个项目列表时,您实际上创建了一个布尔列表列表,这些列表将被传递给 where 子句。设置两个示例列表以突出显示:
q)show instr_id:-5?10
2 8 0 1 6
q)show tp_time:5?.z.t
01:05:42.696 03:42:39.320 17:44:49.101 15:01:01.470 05:47:49.777
运行 你的第一个条件 instr_id = id
原子列表:
q)instr_id=2
10000b
q)instr_id=2 8
'length
[0] instr_id=2 8
^
使用 =
仅适用于长度等于 instr_id
的列表的原子。如果你想传递多个项目来匹配你可以使用 in
:
q)instr_id in 2
10000b
q)instr_id in 2 8
11000b
对于第二个条件 tp_time < time
再次要求传递相同长度的原子或列表:
q)tp_time<.z.t
11111b
q)tp_time<.z.t,.z.t
'length
[0] tp_time<.z.t,.z.t
^
可以通过使用副词来解决这个问题,例如 each-right /:
将 tp_time
与多个项目进行比较,并使用 any
将其缩减为单个列表:
q)tp_time</:.z.t,.z.t
11111b
11111b
q)any tp_time</:.z.t,.z.t
11111b
希望这可以帮助您了解您 运行 遇到上述问题的地方。
我有一个函数声明:
func:{[id;time] select last synp from synp_t where instr_id = id, tp_time < time}
其中 instr_id
的类型为 i
,tp_time
的类型为 v
。
例如,func[8;05:00:11]
工作正常并给我值 17.55
。
但是,如果我尝试 func[8 1;05:00:11 07:10:59]
,我得到:
'length ERROR: incompatible lengths (different lengths of operands for synchronized operation or table columns lengths are not the same
我想得到的是,比如17.55 9.66
.
如果我执行 select res:func_demo[id;time]from tab
,也会弹出同样的错误,其中 tab
是 table,有两列 instr_id
和 tp_time
。
我想 enlist
可能会解决问题,但我不知道如何使用它。我该如何解决这个问题?
func'[8 1;05:00:11 07:10:59]
应该可以
它使用 each both 副词,它将在两个输入列表的相应元素之间应用函数
可以在此处找到有关副词的更多信息http://code.kx.com/q/ref/adverbs/。他们很强大。
许多内置函数都能够将原子和列表作为输入并相应地工作,但是对于用户定义的函数,您很可能需要利用副词在各种情况下使用它们
我建议使用 aj
来获取结果,查看您的查询,您似乎想要给定 id
的最后价格并且小于输入 time
.
q)func:{[id;time] aj[`instr_id`tp_time; ([] instr_id:id;tp_time: time);synp_t]}
q)func[1 1 2;05:00:11 07:10:59 10:10:00]
与 each-both
你正在调用函数 n 次并选择数据 n 次。
aj
是 Kdb 提供的强大功能之一 asof
价格。
其实不需要func
函数,直接用aj
即可得到结果。
aj[`instr_id`tp_time; update instr_id:id, tp_time: time from tab;synp_t]
这里的两个答案都很好,但没有详细说明为什么会出现 'length
错误。错误全归咎于您的 where
子句 where instr_id = id, tp_time < time
。当您传递一个项目列表时,您实际上创建了一个布尔列表列表,这些列表将被传递给 where 子句。设置两个示例列表以突出显示:
q)show instr_id:-5?10
2 8 0 1 6
q)show tp_time:5?.z.t
01:05:42.696 03:42:39.320 17:44:49.101 15:01:01.470 05:47:49.777
运行 你的第一个条件 instr_id = id
原子列表:
q)instr_id=2
10000b
q)instr_id=2 8
'length
[0] instr_id=2 8
^
使用 =
仅适用于长度等于 instr_id
的列表的原子。如果你想传递多个项目来匹配你可以使用 in
:
q)instr_id in 2
10000b
q)instr_id in 2 8
11000b
对于第二个条件 tp_time < time
再次要求传递相同长度的原子或列表:
q)tp_time<.z.t
11111b
q)tp_time<.z.t,.z.t
'length
[0] tp_time<.z.t,.z.t
^
可以通过使用副词来解决这个问题,例如 each-right /:
将 tp_time
与多个项目进行比较,并使用 any
将其缩减为单个列表:
q)tp_time</:.z.t,.z.t
11111b
11111b
q)any tp_time</:.z.t,.z.t
11111b
希望这可以帮助您了解您 运行 遇到上述问题的地方。