qSQL · Select · Practice Question
Find the Times of the Minimum and Maximum Prices
Question
Given the following table of prices throughout the trading day:
p:([] time:12:00:00 13:00:00 14:00:00 15:00:00 16:00:00;
price:3.9 5.1 5.0 4.0 1.7)Result:
time price
--------------
12:00:00 3.9
13:00:00 5.1
14:00:00 5
15:00:00 4
16:00:00 1.7Return:
minTime— the time at which the minimum price occurredmaxTime— the time at which the maximum price occurred
The result may be returned as either a table or a dictionary.
Expected result:
minTime maxTime
----------------
16:00:00 13:00:00Solution
select
minTime:time where price=min price,
maxTime:time where price=max price
from pResult:
minTime maxTime
----------------
16:00:00 13:00:00Alternative Solution
`minTime`maxTime!p[`time] p[`price]?(min;max)@\:p[`price]Result:
minTime| 16:00:00
maxTime| 13:00:00Explanation
The query:
select
minTime:time where price=min price,
maxTime:time where price=max price
from puses a conditional expression within each select clause.
Finding the Minimum Price
The expression:
min pricereturns:
1.7The expression:
time where price=min pricereturns:
16:00:00because that is the time associated with the minimum price.
Finding the Maximum Price
Similarly:
max pricereturns:
5.1and:
time where price=max pricereturns:
13:00:00Handling Duplicate Prices
One advantage of this approach is that it naturally handles duplicate minimum or maximum values.
For example:
p2:([] time:12:00:00 13:00:00 14:00:00;
price:1.7 5.1 1.7)Then:
exec time where price=min price from p2returns:
12:00:00 14:00:00because both rows contain the minimum price.
If only a single result is required, first can be used:
select
minTime:first time where price=min price,
maxTime:first time where price=max price
from p2first also makes each result column a simple list of times. Without it, each cell holds a one-item list (meta shows the column type as V), even though the console display looks the same.
Alternative Solution
The expression:
(min;max)@\:p[`price]returns:
1.7 5.1The Find operator:
p[`price]?1.7 5.1returns the indices of those values:
4 1Those indices are then used to index into the time column:
p[`time] 4 1Result:
16:00:00 13:00:00Finally, `minTime`maxTime! pairs the values with descriptive keys, producing:
minTime| 16:00:00
maxTime| 13:00:00Summary
This question demonstrates two useful approaches:
- Using
whereinside a select statement to locate rows matching an aggregate value. - Using
min,max, and?together to locate the positions of extreme values.
The qSQL solution is generally the most readable and naturally handles cases where multiple rows share the same minimum or maximum price.