gpt4 book ai didi

mysql - 这是一个计算 mysql 表中值中位数的代码,我需要一些专家来解释命令执行的顺序

转载 作者:行者123 更新时间:2023-11-30 22:15:19 25 4
gpt4 key购买 nike

 set @ct :=(select count(*) from medi);
set @ro :=0;
select avg(num) as median from (select * from medi order by num)
where (select @ro:= @ro+1)
between @ct/2.0 AND @ct/2.0+1;

表格中的值
+------+
|数量 |
+------+
| 2 |
| 55 |
| 63 |
| 85 |
| 32 |
| 15 |
| 3 |
| 36 |
| 69 |
+------+
需要帮助来理解执行顺序和 @ro 在 where 子句中的作用

最佳答案

首先,这个查询会产生不正确的结果。它产生了 32 个中位数,这是不正确的。中位数应该是36

The median is the value separating the higher half of a data sample, a population, or a probability distribution, from the lower half. In simple terms, it may be thought of as the "middle" value of a data set. For example, in the data set {1, 3, 3, 5, 9, 11, 13}, the median is 5, the fourth number in the sample. The median is a commonly used measure of the properties of a data set in statistics and probability theory.

引用:https://en.wikipedia.org/wiki/Median

这段 SQL 的问题在于它没有对确定中位数所需的值进行排序。

这个 stackoverflow 问答:Simple way to calculate median with MySQL有很多不同的方法来计算中位数并给出解释。

如果你真的想知道上面的查询是干什么的
Step1:统计表中的行数
Step2:创建一个变量并将其赋值为零
第 3 步:使用变量作为每行递增 1 的数字来查找表格的中间标记。取中间标记旁边的数字的平均值。

现在你会明白为什么它是错误的,它没有对表的内容强加任何顺序。

关于mysql - 这是一个计算 mysql 表中值中位数的代码,我需要一些专家来解释命令执行的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38494928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com