0

I am trying to get information from column_1 and column_2 for apptype CASPER and FOO. The script works fine when I search for just one or the other apptype. However, when use the 'or' statement to include both apptypes in one output, I get an error. I think it is just the 'kdb or' statement.

I don't have a KDB reference manual.

raze{[tradedate] 

setdate tradedate;

`rootordid`clordid xasc

select from( 
        (select column_1, apptype, column_3, from orders where apptype like "CASPER" or "FOO")
         )}each .utl.get_bdts[2017.12.04;2017.12.05]
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
capser
  • 2,442
  • 5
  • 42
  • 74

2 Answers2

4

Rewrite the or statement as follows:

select column_1, apptype, column_3, from orders where (apptype like "CASPER") or apptype like "FOO"

The parenthesis surrounding (apptype like "CASPER") ensure that a length error is not thrown due to the fact that q will read the clause from right to left.

jomahony
  • 1,662
  • 6
  • 9
3

You can make use of any and each right /: to include multiple terms instead of or:

select column_1, apptype, column_3 from orders where any apptype like/:("CASPER";"FOO")

The advantage of this is that it is quite easy to extend for more than 2 terms.

In your example above "CASPER" or "FOO" is being evaluated first which is resulting in a 'length error.

Example:

q)show tab:([]a:1 2 3;b:`CASPER`FOO`BAR)
a b
--------
1 CASPER
2 FOO
3 BAR
q)select from tab where any b like/:("CASPER";"FOO")
a b
--------
1 CASPER
2 FOO
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
  • 1
    This answer is so much better than the accepted. Not only is it more efficient, it's more q-like which speaks to the entire purpose of vector-based languages – Fueled By Coffee Dec 03 '19 at 16:14