0

I would like to convert the following query into a single query

tab:([price:til 10]side:10?-1 1;qty:990 + 2 * til 10);


a:select[>price] from tab where side=1;
b:select from a where sums[qty] <= max(min[sums qty];100);

or

select from (select[>price] from tab where side=1) where sums[qty] <= max(min[sums qty];100);

I have tried to implement a select statement using fby as follows:

select from tab where side=-1, ({sy:sums[y]; sy<=max(min[sy];x)}[100];qty) fby side

However this doesn't sort the table tab correctly i.e. according to >price. I suppose one could pre sort the table i.e.

select from `price xasc tab ...  

However this would seem a computationally inefficient solution. Could some wise Q God please advise me on how to most efficiently achieve this. Thanks, Best Regards

James
  • 1,260
  • 13
  • 25

2 Answers2

2

If the table needs to be sorted in descending order according to price for the sums qty calculation, then using xdesc or desc seems like an unavoidable step.

This is the most succinct version of your query I could find.

select from desc tab where side=1,sums[qty]<=max(min sums qty;100)
Cathal O'Neill
  • 2,522
  • 1
  • 6
  • 17
1

I'm struggling to understand why you are doing the calculation in this way. The sorting will have an impact as it will affect which value is first before the quantities get progressively larger with sums. Is there a reason you aren't using something like this:

q)select from tab where qty = max qty
price| side qty
-----| ---------
9    | 1    1008
q)select from tab where qty = (max;qty) fby side
price| side qty
-----| ---------
8    | -1   1006
9    | 1    1008
Matt Moore
  • 2,705
  • 6
  • 13