How does one accomplish a reverse rank
in an fby
?
In the below, how would we select the two largest values of col2
by date
?
q)dt: 2024.05.13 2024.05.17 2024.05.16 2024.05.17 2024.05.14 2024.05.13 2024.05.16 2024.05.16 2024.05.15 2024.05.16 2024.05.16 2024.05.14 2024.05.13 2024.05.15 2024.05.16 2024.05.14 2024.05.13 2024.05.14
q)col1: 0 13 45 19 30 72 54 4 55 40 46 62 5 18 88 92 20 27
q)col2: 9107 9325 3813 9198 9227 8418 7868 3945 9840 1227 5613 5591 8140 9703 1225 8801 5940 9433
q)t: ([] date: dt; col1:col1; col2:col2)
q)t
date col1 col2
--------------------
2024.05.13 0 9107
2024.05.17 13 9325
2024.05.16 45 3813
2024.05.17 19 9198
2024.05.14 30 9227
2024.05.13 72 8418
2024.05.16 54 7868
2024.05.16 4 3945
2024.05.15 55 9840
2024.05.16 40 1227
2024.05.16 46 5613
2024.05.14 62 5591
2024.05.13 5 8140
2024.05.15 18 9703
2024.05.16 88 1225
2024.05.14 92 8801
2024.05.13 20 5940
2024.05.14 27 9433
This attempt didn’t work. The value for 2024.05.13 is correct, but the other dates are incorrect.
q)select from t where 0=(idesc;col2) fby date
date col1 col2
--------------------
2024.05.13 0 9107
2024.05.17 13 9325
2024.05.15 55 9840
2024.05.16 40 1227
2024.05.14 62 5591
q)`date`col2 xdesc t
date col1 col2
--------------------
2024.05.17 13 9325
2024.05.17 19 9198
2024.05.16 54 7868
2024.05.16 46 5613
2024.05.16 4 3945
2024.05.16 45 3813
2024.05.16 40 1227
2024.05.16 88 1225
2024.05.15 55 9840
2024.05.15 18 9703
2024.05.14 27 9433
2024.05.14 30 9227
2024.05.14 92 8801
2024.05.14 62 5591
2024.05.13 0 9107
2024.05.13 72 8418
2024.05.13 5 8140
2024.05.13 20 5940
(1) Why didn’t the original fby
work?
(2) What does work to accomplish the reverse rank?