Search code examples
kdb

How do I query a list in a field?


I have a table of the form:

t                             p      s    c
----------------------------------------------------------
2019.08.06D13:29:45.746200000 4.5    4    (," ";,"T";,"I")
2019.08.06D13:29:58.413500000 4.5148 14   (," ";,"T";,"I")
2019.08.06D13:30:00.072000000 4.65   1522 (," ";,"O")
2019.08.06D13:30:03.681300000 4.5048 1    (," ";,"I")
2019.08.06D13:30:04.045200000 4.5    5    (," ";,"I")

I'm trying to grab the row at 13:00, that contains "O" in the column c.

I think it should be something like:

select from t where "O" in c

but it doesn't seem to work (null result). I've tried raze on this column too, but it doesn't help. What am I doing wrong?

Edit with raze each c:

2019.08.06D13:29:45.746200000 4.5    4    " TI"
2019.08.06D13:29:58.413500000 4.5148 14   " TI"
2019.08.06D13:30:00.072000000 4.65   1522 " O"
2019.08.06D13:30:03.681300000 4.5048 1    " I"
2019.08.06D13:30:04.045200000 4.5    5    " I"

Solution

  • You need to enlist"0" as that is the form the c values take. In combination with each-right (/:) you can check if enlist"0" is in each c value:

    q)t:([]c:(enlist each(" ";"T";"I");enlist each(" ";"T";"I");enlist each(" ";"O");enlist each(" ";"I");enlist each(" ";"I")))
    q)select from t where enlist["O"]in/:c
    c
    ---------
    ," " ,"O"
    

    No need for the enlist if you raze the result:

    q)select from(update raze each c from t)where"O"in/:c
    c
    ----
    " O"