HowTo calculate percentile (0.5) (known as median) with windowing clause
Hallo,
i have a list of values of i.e. pH-values or temperature values taken on different date (time, day) in DD.MM.YY format, i.E
01.01.2014: pH 4
03.01.2014: ph 5
01.02.2014: ph 5
03.05.2015: ph 6
03.01.2015: ph 7
05.01.2015: ph 7
I have to calculate the median (percentile_cont(0.5)) within a timewindow of 1 year, i.E:
01.01.2014: pH 4: median 4
03.01.2014: ph 5: median 4.5
01.02.2014: ph 5: median 5
03.05.2014: ph 6: median 5
03.01.2015: ph 7: median 6 !! Values from 01.02.2014, 03.05.2014 and 03.01.2015
05.01.2015: ph 7: median 7 !! Values from 03.05.2014, 03.01.2015 and 05.01.2015
So it is percentile calculation within a time window. I was able to calculate it with trivial SQL using subselects within the select part of the main query, but it was to slow.
I have to select from a large table with many rows and many values for each ph-value within a year. So I have to decide whether to use a) pre-calculated median values using trigger technology, b) using materialized views or c) using analytic function.