In DolphinDB, how to set multiple query conditions as np.select() does?

27 Views Asked by At

To set multiple query conditions, the CASE WHEN statement can be used in SQL in the case of a specified number of conditions; otherwise, the coalesce function can be used for dynamic SQL queries. I wonder how to perform multiple-condition queries with DolphinDB scripting language.

The following script is written in the Q language:

def^(^/)?'[conds;choices;nil]

I use metaprogramming to achieve multiple-condition queries in DolphinDB, as shown below:

unifedCall(coalesce, iif:E(conds. choices, nil).matrix2tuple()).nullFill(nil)

I wonder how to convert a matrix into a tuple or apply the coalesce function to a matrix with DolphinDB scripts.

1

There are 1 best solutions below

0
Claire mcc On

To achieve multiple-condition queries using metaprogramming, you can refer to the following script:

unifiedCall(coalesce, loop(iif, conds, choices, NULL)).nullFill!(default)

conds is a list of multiple conditions and choices is a list of arrays from which the elements are selected based on the corresponding true conditions in conds. The loop template always returns a tuple. The input parameter args of the unifiedCall function is a tuple. You can use this feature to generate metacode.