Cálculo da mediana em Sap Hana

Uma ideia muito boa, encontrada em
http://sqlblog.com/blogs/adam_machanic/archive/2006/12/18/medians-row-numbers-and-performance.aspx

SELECT
CustomerId,
AVG
(TotalDue)
FROM

(
SELECT

CustomerId,
TotalDue,
ROW_NUMBER
() OVER (
PARTITION BY
CustomerId
ORDER BY
TotalDue ASC, SalesOrderId ASC) AS RowAsc,
ROW_NUMBER
() OVER (
PARTITION BY
CustomerId
ORDER BY
TotalDue DESC, SalesOrderId DESC) AS RowDesc
FROM
Sales.SalesOrderHeader SOH
) x
WHERE

RowAsc IN (RowDesc, RowDesc - 1, RowDesc + 1)
GROUP BY
CustomerId
ORDER BY
CustomerId