本文介绍了如何提取中值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在中值"列中获取中值.有什么想法吗?

I need to get median value in column "median". Any ideas, please?

SELECT
MIN(score) min, CAST(AVG(score) AS float) median, MAX(score) max
FROM result JOIN student ON student.id = result.student_id

推荐答案

我觉得最简单的方法是PERCENTILE_CONT()或者PERCENTILE_DISC():

I think the simplest method is PERCENTILE_CONT() or PERCENTILE_DISC():

SELECT MIN(score) as min_score,
       PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY score) as median_score,
       MAX(score) max_score
FROM result r JOIN
     student s
     ON s.id = r.student_id;

这假设(合理地)score 是数字.

This assumes (reasonably) that score is numeric.

PERCENTILE_CONT()PERCENTILE_DISC() 之间的区别是当有偶数个值时会发生什么.这通常是一个不重要的考虑因素,除非您有少量数据.

The difference between PERCENTILE_CONT() and PERCENTILE_DISC() is what happens when there are an even number of values. That is usually an unimportant consideration, unless you have a small amount of data.

这篇关于如何提取中值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:44