MichaelGuest
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
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”.