stats: percentile and median impl + minor refactoring to mode

example:

***select percentile(score, 0.25) as firstq, median(score), percentile(score, 0.75) as thirdq from events***

see #2624