Check presence of a column in multiple tables

时间:2018-09-18 08:40:17

标签: kdb

I want to check if a column is present in multiple tables. When I try for one table, it works.

`tickerCol in cols tradeTable / (output is 1b) hence working perfectly
`tickerCol in cols table2 / (output is 1b) hence working perfectly

but when I run

`ticker in cols @' (tradeTable;table2) / (output is 0b, expected output 11b)

for above example ticker column is present in both tables(tradeTable;table2).

2 个答案:

答案 0 :(得分:4)

The following works using each-both ':

`ticker in ' cols each (tradeTable; table2)

This will find the columns that are present in each of the tables and then perform a check on each of the column lists to find if `ticker is present in these lists.

答案 1 :(得分:2)

解决方案已在另一个答案中提供。只是试图解释为什么您的解决方案无法正常工作。

假设我们有2个表t1(列idv1)和t2(列idv2)。 / p>

现在,当我们运行时:

    q) cols@'`t1`t2

输出将是列表的列表:

(`id`v1;`id`v2)

此列表有2个条目,每个条目都是一个列表。

现在您正在做的是尝试在此列表中查找列。

    q) `id in (`id`v1;`id`v2)   /output 0b

并且由于该列表没有id作为条目,因此它返回0b

如果您搜索`id`v1(这是一个列表),则会得到1b个匹配的第一项。

    q)  `id`v1 in (`id`v1;`id`v2)  / output 1b

这里想要的是在该列表的每个条目中搜索列名称。因此,表达中唯一缺少的是两者。这将起作用:

     q) `id in'cols@'`t1`t2   / output 11b

您的情况将是:

     q) `ticker in ' cols@'`tradeTable`table2