I'm solving the classic problem of fby to find the max price per symbol from a trade table.
table: tr
time sym src price size
-------------------------------------------------
2019.03.11D09:00:00.277000000 GOOG L 36.01 1427
2019.03.11D09:00:04.123000000 GOOG O 36.01 708
2019.03.11D09:00:08.123000000 MSFT N 35.5 7810
2019.03.11D09:00:10.123000000 MSFT O 31.1 1100
when I apply fby:
select from tr where price=(max;price) fby sym
Output is:
time sym src price size
-------------------------------------------------
2019.03.11D09:00:00.277000000 GOOG L 36.01 1427
2019.03.11D09:00:04.123000000 GOOG O 36.01 708
2019.03.11D09:00:08.123000000 MSFT N 35.5 7810
But, as we can see I'm getting sym GOOG trade twice, since max price is same. Hence now I want to get the output per sym with last traded time of each symbol(along with max price). So, I use below query
select from (select from tr where price=(max;price) fby sym) where time=(last;time) fby sym
to get the ouput:
time sym src price size
-------------------------------------------------
2019.03.11D09:00:04.123000000 GOOG O 36.01 708
2019.03.11D09:00:08.123000000 MSFT N 35.5 7810
Is there any better/optimized way to write above query which is not using select/fby twice?