PDF newb
PDF newb

Reputation: 1

Turning Percentile_cont/disc (Median) into scalar function

Editing this since turns out we were trying to recreate the wheel.

The below works perfectly in determining the median. Now, how would we go about converting into a function so that we can call median(column) instead of having to do the below each time. Below does the trick:

   select percentile_cont(0.5) within group (order by n) over (PARTITION BY [column1]),

   from t;

AHH - I see. Is it possible to groupby where it calcs the median only across where column1 = a,b,c so output would be

 A median of values with A identifier
 B median of values with B identifier
 C median of values with C identifier

Upvotes: 0

Views: 58

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

You should just usetthe percentile_cont() or percentile_disc() window functions:

select percentile_cont(0.5) within group (order by n) over (),
       percentile_disc(0.5) within group (order by n) over ()      
from t;

There is no need to re-invent the wheel.

Upvotes: 3

Related Questions