Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query KDB table where one column is a list?

Tags:

kdb

I have a table in KDB where one of the columns is a list of symbols, for example:-

q)table:([id:1 2 3 4 5] books:((`book1`book2);(enlist `book6);(enlist`book3);(enlist`book4);(`book2;`book5)))
q)table
id| books
--| ------------
1 | `book1`book2
2 | ,`book6
3 | ,`book3
4 | ,`book4
5 | `book2`book5
q)

I want to find all the rows where `book2 is one of the books in the books column, how do I do this? So in the example above, I'd expect the 1st and 5th rows to be returned. I have tried various things using the "in" operator but either get an error or no results, for example :-

q)select from books where `book2 in books
clientTradeId| book
-------------| ----
q)select from books where enlist `books2 in books
'length
like image 741
Alan Chan Avatar asked Mar 10 '23 21:03

Alan Chan


2 Answers

select from table where `book2 in/:books

is valid, using the eachright adverb.

like image 176
Paul Kerrigan Avatar answered Mar 14 '23 07:03

Paul Kerrigan


select from table where `book2 in'books

This utilises the adverb each-both, read more about it here: http://code.kx.com/q4m3/6_Functions/#672-each-both

like image 22
jomahony Avatar answered Mar 14 '23 09:03

jomahony