Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert this nested query into a single by query (simulating a MySql window function)?

Given the following

\S 1234
t:([]a:10 ? til 5; b:10 ? til 100; c:10 ? til 10000);

select a,b,c from ij[t; select maxc:max c by a from t] where c=maxc;

Instead of joining an inner query to use as part of the filter, is it possible to have q select the first record in each (window grouped by a and sorted by c descending)? If so, no join would be necessary.

The result would be the same. I could then do testing to see if performance if different. But mainly just trying to understand what kinds of queries are and are not possible.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could try fby which matches the result of your current query

q)select from t where c = (max;c) fby a
a b  c
---------
2 2  5480
3 39 9473
4 77 6458

https://code.kx.com/q/ref/fby/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading