将迭代函数的输出存储到唯一表 KDB+/Q
Storing output of a iterating function into unique tables KDB+/Q
我有格式的查询功能:
function:{[x] select Y from table1 where date=x}
我想在存储在单独 table 中的所有日期中迭代此函数,就像这样
function each (select distinct date from table2)
并将每次迭代存储到它自己的、唯一的 table 中。例如,如果第一次迭代是 2021.11.10,则该日期来自 table1 的所有 'Y' 值都存储在名为 'a' 的 table 中,用于下一次迭代对于日期 2021.11.12,它转到名为 'b' 的 table 或类似的名称。我该怎么做?
假设下表:
q)t1:([]date:.z.d + 0 0 0 1 1 1;a:1 2 3 4 5 6)
q)t2:([]date:.z.d+0 1)
然后你可以这样做:
q)function:{[x;y] x set select from t1 where date=y}
q)function'[`a`b;exec distinct date from t2]
`a`b
q)show a
date a
------------
2021.11.10 1
2021.11.10 2
2021.11.10 3
q)show b
date a
------------
2021.11.11 4
2021.11.11 5
2021.11.11 6
如果你想让它更有活力一点,那么你也可以这样做:
q)function:{[x] (`$"tab",string[x] except ".") set select from t1 where date=x}
q)
q)
q)function each exec distinct date from t2
`tab20211110`tab20211111
q)show tab20211110
date a
------------
2021.11.10 1
2021.11.10 2
2021.11.10 3
q)show tab20211111
date a
------------
2021.11.11 4
2021.11.11 5
2021.11.11 6
以上创建了以每个日期命名的新表(我们删除了结果名称中的“.”,因为它们可能会与 q 的 .
运算符混淆)
我有格式的查询功能:
function:{[x] select Y from table1 where date=x}
我想在存储在单独 table 中的所有日期中迭代此函数,就像这样
function each (select distinct date from table2)
并将每次迭代存储到它自己的、唯一的 table 中。例如,如果第一次迭代是 2021.11.10,则该日期来自 table1 的所有 'Y' 值都存储在名为 'a' 的 table 中,用于下一次迭代对于日期 2021.11.12,它转到名为 'b' 的 table 或类似的名称。我该怎么做?
假设下表:
q)t1:([]date:.z.d + 0 0 0 1 1 1;a:1 2 3 4 5 6)
q)t2:([]date:.z.d+0 1)
然后你可以这样做:
q)function:{[x;y] x set select from t1 where date=y}
q)function'[`a`b;exec distinct date from t2]
`a`b
q)show a
date a
------------
2021.11.10 1
2021.11.10 2
2021.11.10 3
q)show b
date a
------------
2021.11.11 4
2021.11.11 5
2021.11.11 6
如果你想让它更有活力一点,那么你也可以这样做:
q)function:{[x] (`$"tab",string[x] except ".") set select from t1 where date=x}
q)
q)
q)function each exec distinct date from t2
`tab20211110`tab20211111
q)show tab20211110
date a
------------
2021.11.10 1
2021.11.10 2
2021.11.10 3
q)show tab20211111
date a
------------
2021.11.11 4
2021.11.11 5
2021.11.11 6
以上创建了以每个日期命名的新表(我们删除了结果名称中的“.”,因为它们可能会与 q 的 .
运算符混淆)