title: How to calculate moving median with percentile_disc() and window?
body: <p>The window is working with COUNT, AVG, etc. but not works with percentile_disc</p>
  
  <pre><code>  SELECT
         x,
         COUNT(*) OVER w AS w_count, -- fine
         AVG(x) OVER w   AS avg_x,     -- fine
         percentile_disc(0.5) within group (order by x) OVER w AS mdn_x  -- BUG!
    FROM t
    WINDOW w AS (ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)
    ORDER BY 1
  </code></pre>
  
  <p>(edited), </p>
  
  <ul>
  <li>PostgreSQL v10.12  say   <em>syntax error at or near "OVER"</em>.</li>
  <li>PostgreSQL v12.2  say <em>OVER is not supported for ordered-set aggregate percentile_disc</em></li>
  </ul>
  
  <p>Seems that is not possible... There are some workaround? Perhaps a lateral join with a subquery.</p>
  
  <hr>
  
  <p>As <a href="https://anomaly.io/moving-median-robust-anomaly/index.html" rel="nofollow noreferrer">explained here</a>, <strong>the MEDIAN is important</strong>, better tham AVG in anomaly analysis. As well the <a href="https://en.wikipedia.org/wiki/Moving_average#Moving_median" rel="nofollow noreferrer">moving median</a>, better (most resilient) tham moving average.</p>