<legend id='Cu2qH'><style id='Cu2qH'><dir id='Cu2qH'><q id='Cu2qH'></q></dir></style></legend>

        • <bdo id='Cu2qH'></bdo><ul id='Cu2qH'></ul>
      1. <small id='Cu2qH'></small><noframes id='Cu2qH'>

        <tfoot id='Cu2qH'></tfoot>
        <i id='Cu2qH'><tr id='Cu2qH'><dt id='Cu2qH'><q id='Cu2qH'><span id='Cu2qH'><b id='Cu2qH'><form id='Cu2qH'><ins id='Cu2qH'></ins><ul id='Cu2qH'></ul><sub id='Cu2qH'></sub></form><legend id='Cu2qH'></legend><bdo id='Cu2qH'><pre id='Cu2qH'><center id='Cu2qH'></center></pre></bdo></b><th id='Cu2qH'></th></span></q></dt></tr></i><div id='Cu2qH'><tfoot id='Cu2qH'></tfoot><dl id='Cu2qH'><fieldset id='Cu2qH'></fieldset></dl></div>

        用 MySQL 计算中位数的简单方法

        时间:2023-08-18

        <i id='bLgCP'><tr id='bLgCP'><dt id='bLgCP'><q id='bLgCP'><span id='bLgCP'><b id='bLgCP'><form id='bLgCP'><ins id='bLgCP'></ins><ul id='bLgCP'></ul><sub id='bLgCP'></sub></form><legend id='bLgCP'></legend><bdo id='bLgCP'><pre id='bLgCP'><center id='bLgCP'></center></pre></bdo></b><th id='bLgCP'></th></span></q></dt></tr></i><div id='bLgCP'><tfoot id='bLgCP'></tfoot><dl id='bLgCP'><fieldset id='bLgCP'></fieldset></dl></div>
      2. <small id='bLgCP'></small><noframes id='bLgCP'>

          <tfoot id='bLgCP'></tfoot>

            1. <legend id='bLgCP'><style id='bLgCP'><dir id='bLgCP'><q id='bLgCP'></q></dir></style></legend>
                <bdo id='bLgCP'></bdo><ul id='bLgCP'></ul>
                  <tbody id='bLgCP'></tbody>

                  本文介绍了用 MySQL 计算中位数的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  使用 MySQL 计算中位数的最简单(希望不会太慢)方法是什么?我已经使用 AVG(x) 来找到平均值,但我很难找到一种计算中位数的简单方法.现在,我将所有行返回给 PHP,进行排序,然后选择中间的行,但肯定有一些简单的方法可以在单个 MySQL 查询中执行此操作.

                  What's the simplest (and hopefully not too slow) way to calculate the median with MySQL? I've used AVG(x) for finding the mean, but I'm having a hard time finding a simple way of calculating the median. For now, I'm returning all the rows to PHP, doing a sort, and then picking the middle row, but surely there must be some simple way of doing it in a single MySQL query.

                  示例数据:

                  id | val
                  --------
                   1    4
                   2    7
                   3    2
                   4    2
                   5    9
                   6    8
                   7    3
                  

                  val 排序给出 2 2 3 4 7 8 9,所以中位数应该是 4,而 SELECT AVG(val) which == 5.

                  Sorting on val gives 2 2 3 4 7 8 9, so the median should be 4, versus SELECT AVG(val) which == 5.

                  推荐答案

                  在 MariaDB/MySQL 中:

                  In MariaDB / MySQL:

                  SELECT AVG(dd.val) as median_val
                  FROM (
                  SELECT d.val, @rownum:=@rownum+1 as `row_number`, @total_rows:=@rownum
                    FROM data d, (SELECT @rownum:=0) r
                    WHERE d.val is NOT NULL
                    -- put some where clause here
                    ORDER BY d.val
                  ) as dd
                  WHERE dd.row_number IN ( FLOOR((@total_rows+1)/2), FLOOR((@total_rows+2)/2) );
                  

                  Steve Cohen 指出,在第一遍之后,@rownum 将包含总行数.这可用于确定中位数,因此不需要第二遍或连接.

                  Steve Cohen points out, that after the first pass, @rownum will contain the total number of rows. This can be used to determine the median, so no second pass or join is needed.

                  还有 AVG(dd.val)dd.row_number IN(...) 用于在记录数为偶数时正确生成中位数.推理:

                  Also AVG(dd.val) and dd.row_number IN(...) is used to correctly produce a median when there are an even number of records. Reasoning:

                  SELECT FLOOR((3+1)/2),FLOOR((3+2)/2); -- when total_rows is 3, avg rows 2 and 2
                  SELECT FLOOR((4+1)/2),FLOOR((4+2)/2); -- when total_rows is 4, avg rows 2 and 3
                  

                  最后,MariaDB 10.3.3+ 包含一个 MEDIAN 函数

                  这篇关于用 MySQL 计算中位数的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:MySQL LIKE IN()? 下一篇:从 MySQL 中的分层数据生成基于深度的树(无 CTE)

                  相关文章

                  最新文章

                • <small id='OTdCw'></small><noframes id='OTdCw'>

                    • <bdo id='OTdCw'></bdo><ul id='OTdCw'></ul>
                  1. <tfoot id='OTdCw'></tfoot>
                  2. <legend id='OTdCw'><style id='OTdCw'><dir id='OTdCw'><q id='OTdCw'></q></dir></style></legend>
                  3. <i id='OTdCw'><tr id='OTdCw'><dt id='OTdCw'><q id='OTdCw'><span id='OTdCw'><b id='OTdCw'><form id='OTdCw'><ins id='OTdCw'></ins><ul id='OTdCw'></ul><sub id='OTdCw'></sub></form><legend id='OTdCw'></legend><bdo id='OTdCw'><pre id='OTdCw'><center id='OTdCw'></center></pre></bdo></b><th id='OTdCw'></th></span></q></dt></tr></i><div id='OTdCw'><tfoot id='OTdCw'></tfoot><dl id='OTdCw'><fieldset id='OTdCw'></fieldset></dl></div>