First, the columns are boolean vectors, so you can use them ‘raw’ in the where
clause:
q)tbl
red blue green yellow white purple
----------------------------------
0 0 1 0 0 0
1 0 1 1 0 0
1 0 1 1 1 0
0 0 0 0 0 0
0 1 0 0 1 1
1 0 1 1 0 0
q)select from tbl where red or green
red blue green yellow white purple
----------------------------------
0 0 1 0 0 0
1 0 1 1 0 0
1 0 1 1 1 0
1 0 1 1 0 0
Can you use your own function in the where
clause? Absolutely.
q)isRG:{or/[x`red`green]}
q)isRG tbl
111001b
q)select from tbl where isRG tbl
red blue green yellow white purple
----------------------------------
0 0 1 0 0 0
1 0 1 1 0 0
1 0 1 1 1 0
1 0 1 1 0 0
Going beyond your question, to make the column names arguments to your function, instead of writing a function to use in the q-SQL where
clause, use functional select. In this, you express your constraint as a parse tree, e.g. (or;`red;`white)
q)?[tbl; enlist(or; `red; `white); 0b; ()]
red blue green yellow white purple
----------------------------------
1 0 1 1 0 0
1 0 1 1 1 0
0 1 0 0 1 1
1 0 1 1 0 0
You can then parameterise the column names:
q)selEither:{[t; c1; c2] ?[t; enlist(or; c1 ;c2); 0b; ()]}
q)selEither[tbl; `red; `white]
red blue green yellow white purple
----------------------------------
1 0 1 1 0 0
1 0 1 1 1 0
0 1 0 0 1 1
1 0 1 1 0 0
Lastly, you can extend this from a pair to a list of column names:
q)selAny:{[tbl; cn] ?[tbl; enlist(or/;enlist,cn); 0b; ()]}
q)selAny[t; `white`green`yellow]
…
See more at KX Technical Whitepaper Parse Trees and Functional Forms