select NN,min(price/max_peak-1) as max_loss_rate from ( select NN, price, max(price) over (partition by NN order by tddate rows between unbounded preceding and 1 preceding) as max_peak from sam_maxloss)a group by NN;