Skip to content



med x    med[x]

Where x is a numeric list returns its median.

q)med 10 34 23 123 5 56
q)select med price by sym from trade where date=2001.10.10,sym in`AAPL`LEH

med is an aggregate function, equivalent to

{avg x (iasc x)@floor .5*-1 0+count x,:()}

Partitions and segments

med signals a part error when running a median over partitions, or segments. (Since V3.5 2017.01.18; from V3.0 it signalled a rank error.) This is deliberate, as previously med was returning median of medians for such cases. This should now be explicitly coded as a cascading select.

select med price by sym from 
  select price, sym from trade where 
      date within 2001.10.10 2001.10.11, 
      sym in `AAPL`LEH;