1

We have a table t:

t:([] sym:`GOOG`IBM`APPL; px:10 20 30; size:1000 2000 3000)

Now we want to select assign a column in output based on a condition provided in a function.

{[m]select sym, px, size, eb:?[`ab=m;`cd;`ef] from t where size>1000}[`ab] / This works fine providing proper value to eb in output(if/else)

But my requirement is based on (if/else if) to set the value of eb as below, tried ?, $ but didn't work

{[m]select sym, px, size, eb:?[`ab=m;`cd;`yz=m;`ef] from t where size>1000}[`ab] / It fails with type error 

requirement(Sudo code):

if (m==ab) { return cd};
else if (m==yz) {return ef};

2 Answers 2

6

When using vector conditional ? you need to nest the conditionals. In this example not matching either condition will return a null.

q){[m]select sym, px, size, eb:?[`ab=m;`cd;?[`yz=m;`ef;`]] from t where size>1000}[`ab]
sym  px size eb
---------------
IBM  20 2000 cd
APPL 30 3000 cd

If you have a lot of discrete conditions this can become very unwieldy, an alternative would be to use a dictionary.

q)dict:`ab`yz!`cd`ef
q){[m]select sym, px, size, eb:dict[m] from t where size>1000}[`ab]
sym  px size eb
---------------
IBM  20 2000 cd
APPL 30 3000 cd
Sign up to request clarification or add additional context in comments.

Comments

2

You can also use the find [?] operator to define a function to build that column:

q)f:{`cd`ef` `ab`yz?x}
q)f[`ab]
`cd
q)f[`yz]
`ef
q)f[`jk] // no match
`
q)select sym, px, size, eb:f[`ab] from t where size>1000
sym  px size eb
---------------
IBM  20 2000 cd
APPL 30 3000 cd

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.