如何基于列表元素筛选kdb数据表中指定列匹配的行?
Hey there! Since you're new to kdb+, let's break this down step by step to get the rows you need from the run table.
First, let's fix a small possible typo in your list definition: to match the string values in your third column, your list A should be defined as a string list like this:
A:("aa";"bb";"cc")
Now, there are two straightforward ways to filter the run table for rows where the third column value exists in A:
Method 1: Use select with where (most intuitive for beginners)
select from run where third in A
Here, third in A generates a boolean list where each entry corresponds to a row in run—it returns 1b (true) if the row's third value is in list A, and 0b (false) otherwise. The where clause keeps only the rows where the boolean value is 1b.
Method 2: Direct boolean indexing (native kdb+ style)
run[third in A]
This achieves exactly the same result as Method 1. It uses the boolean list from third in A directly as an index to slice the run table, which is a common pattern in kdb+ for more concise operations.
Quick extra tip
If your list A was defined as symbols (e.g., A:aabbcc) but your third column uses strings, just add a type conversion to match them:
select from run where third in string A // Convert symbol list to strings // OR select from run where string third in A // Convert third column to symbols
内容的提问来源于stack exchange,提问作者user8415577

