KDB/Q分区表筛选date列及所有_test后缀列的方法问询
Got it, let's fix this for you. The issue with your original delete all except approach is that kdb+ doesn't support wildcard patterns like *_test directly in the except clause—you need to explicitly generate the list of columns you want to keep first.
Step-by-Step Solution
First, let's get the list of columns that end with _test using cols (to fetch all column names) and like (to filter by the suffix pattern):
test_cols: cols[t] where cols[t] like "*_test"
For your sample table, this will return ,a_testb_test.
Then, select the date column along with these filtered columns in one query:
select date, test_cols from t where date=2019.01.01
One-Liner Version
If you prefer a more concise approach, combine the two steps into a single line:
select date, (cols[t] where cols[t] like "*_test") from t where date=2019.01.01
Alternative Using Indexing
You can also use kdb+'s indexing operator ! to extract the desired columns directly:
t[`date, (cols[t] where cols[t] like "*_test")] where t.date=2019.01.01
Verification with Your Sample Table
Let's test this with your example table:
t:([]date:3#2019.01.01; a:1 2 3; a_test:2 3 4; b_test:3 4 5; c: 6 7 8);
Running any of the above queries will give you the expected output:
date a_test b_test ------------------------ 2019.01.01 2 3 2019.01.01 3 4 2019.01.01 4 5
This method scales perfectly even if you have 100+ _test columns—no need to manually list each one!
内容的提问来源于stack exchange,提问作者Utsav

