cancel
Showing results for 
Search instead for 
Did you mean: 

functions that return functions in a select statement

xinyu_gai
New Contributor
Hi,

I had a problem when applying a function to a select statement. For example, the following code works perfectly 

$[0;max;min] 1 2 3 4 5

but if put the function in a select statement, it doesn't work,eg

q)sp
s  p  qty
---------
s1 p1 300
s1 p2 200
s1 p3 400
s1 p4 200
s4 p5 100
s1 p6 100
s2 p1 300
s2 p2 400
s3 p2 200
s4 p2 200
s4 p4 300
s1 p5 400

q)select $[1;max;min] qty from sp
'rank
  [0]  select $[1;max;min] qty from sp 
-----------------------------------------------------------------------------------------------------------------------------------------------------

Instead of putting the whole thing into {} like this

 {[x;t] $[x;select max qty from t;select min qty from t]}[1;sp]
 
I'm just wondering is their a way to format the query properly that do the job but avoid putting everything into {}? 

Any ideas are greatly appreciated, thanks a lot!

Xinyu


5 REPLIES 5

rathore_ajay
New Contributor

I think Cond is not supported directly inside q-sql (https://code.kx.com/v2/ref/select/), you can use vector conditional instead

q)select enlist ?[1b;max;min]qty from t
qty
---
90

Or use a function/lambda

q)select {enlist $[1b;max;min]x}qty from t
qty
---
90

-Ajay

rahul_asati04
Contributor
You could use following options:

q) select enlist {$[0;max;min] x} qty from sp

or

q) select enlist (max;min)[0] qty from sp

TerryLynch
New Contributor II
I think there's two issues:

1. Use vector conditional (even if you only have a single boolean)
2. The aggregation isn't being "recognised" so you have to manually enlist

/aggregation recognised - atom automatically enlisted
q)select max qty from sp
qty
---
90

/aggregation not recognised - result isn't a list
q)select {max x}qty from sp
'rank
  [0]  select {max x}qty from sp
       ^

/manually enlist to ensure list output
q)select {enlist max x}qty from sp
qty
---
90


Putting it all together

q)select ?[1b;{enlist max x};{enlist min x}]qty from sp
qty
---
90

q)select ?[0b;{enlist max x};{enlist min x}]qty from sp
qty
---
1

KMRPKJ
New Contributor
Hi 

Please try below query.
rase $[1;max;min] select qty from sp

Regards
Pankaj Kumar 

KMRPKJ
New Contributor
Hi 

Try below query.

raze $[0;max;min] select qty from sp