qsql select from or condition gives error - Kdb+ / qStudio

Home Forums Kdb+ / qStudio qsql select from or condition gives error

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #160

    Michael
    Guest

    When I try to run the following query against our dev box I get a type error. What is type?

    q)select Sym,Reg,Alpha from equity_tab where Reg=`P2 or Alpha>10.
    ‘type

    I tried changing the parameters round but get empty results when I know there should be some.
    q)select Sym,Reg,Alpha from equity_tab where Alpha>10. or Reg=`P2
    Sym Reg Alpha
    ————-

    Do you know what’s going on?

    ~Mike

    #206

    admin
    Keymaster

    q code is evaluated right to left. When you say:
    select from t where a=1 or b=`c
    What kdb is evaluating is:
    select from t where a=(1 or (b=`c))
    Which reduces to:
    select from t where a=1

    You can see this from the example below:

    q)t:([] a:5?01b; b:5?`c`d)
    q)t
    a b
    ---
    0 c
    1 c
    1 d
    0 c
    0 d
    q)select from t where a=1 or b=`c
    a b
    ---
    1 c
    1 d
    q)select from t where a=(1 or b=`c)
    a b
    ---
    1 c
    1 d

    To make kdb behave how you expect place parentheses around a=1 like so:

    q)select from t where (a=1) or b=`c
    a b
    ---
    0 c
    1 c
    1 d
    0 c

    There is a similar issue when using “and”, though mostly in kdb you will want to use comma to separate conditions rather than “and”.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.