解释之前回答的帖子的解决方案:在 MySQL 中查找中位数

发布于 2024-12-29 11:08:00 字数 490 浏览 0 评论 0原文

我需要在 MySQL 中计算中值。我在此处看到了解决方案。

但是,我不明白其中的一部分。在此处输入代码提供的解决方案如下:

SELECT x.val from data x, data y
GROUP BY x.val
HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*)+1)/2

在原始问题的上下文中,data xdata y 是什么?通常FROM后跟表名。但是,为什么问题只涉及一张表时却列出了两张表呢?有人可以解释这个解决方案是如何工作的吗?另外,我不明白这部分: HAVING SUM(SIGN(1-SIGN(y.val-x.val)))

I need to calculate the median value in MySQL. I saw the solution here.

But, I didn't understand part of it. The solution provided enter code here is as follows:

SELECT x.val from data x, data y
GROUP BY x.val
HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*)+1)/2

What is data x and data y in the context of the original question? Usually FROM is followed by the table name. But, then why are 2 tables listed when the question refers to only one? Can someone explain how this solution works? Also, I didn't understand this part: HAVING SUM(SIGN(1-SIGN(y.val-x.val))) .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逐鹿 2025-01-05 11:08:00

原始问题中的数据 x 和数据 y 是什么?
通常FROM后跟表名。但是,那为什么有 2 张桌子
当问题仅涉及一个时列出?

在最初的问题中,data x, data y 将表与其自身连接起来,创建笛卡尔积。原始表有 7 行,通过将每一行与其他行连接起来,得到的结果是 49 行。

另外,我不明白这部分:HAVING
SUM(SIGN(1-SIGN(y.val-x.val)))
.

本质上,该函数确定每个值有多少个值小于正在检查的值。然后将该总数与计数的一半 + 1 进行比较,然后选择该值作为中位数。

它通过从其比较的值 (y.val) 中减去值 (x.val) 来实现此目的。然后,它使用 SIGN 函数将结果转换为 -101。然后减去该值,然后再次获取 SIGN。因此,如果 y.val 值小于与之比较的 x.val 值,则最终结果将为 1 。例如,假设 y3x5

3 - 5 = -2
SIGN(-2) = -1
1 - (-1) = 2
SIGN(2) = 1

如果 y5,而 x3...最终结果将为 0< /code>:

5 - 3 = 2
SIGN(2) = 1
1 - 1 = 0
SIGN(0) = 0

对这些比较的结果进行求和,得到一个数字,该数字指示有多少值出现在我们正在检查的值之前。然后,它将这个 SUMCOUNT(*) + 1 / 2 进行比较,以找到中间范围...

What is data x and data y in the context of the original question?
Usually FROM is followed by the table name. But, then why are 2 tables
listed when the question refers to only one?

In the original question, data x, data y joins the table to itself, creating a cartesian product. The original table had 7 rows, and by joining every row against every other row, the resulting product is 49 rows.

Also, I didn't understand this part: HAVING
SUM(SIGN(1-SIGN(y.val-x.val)))
.

Essentially, this function determines for every value how many values are less than the one being examined. It then compares this total to half the count + 1... and then selects that value as the median.

It does this by subtracting the value (x.val) from the value it is comparing (y.val). It then uses the SIGN function to convert the result to -1, 0, or 1. It then subtracts this value, and then takes the SIGN again. So if the y.val value is less than the x.val value it that is being compared to, the end result would be a 1. For example, let's say y is 3, and x is 5.

3 - 5 = -2
SIGN(-2) = -1
1 - (-1) = 2
SIGN(2) = 1

If y were 5, and x was 3... the end result would be 0:

5 - 3 = 2
SIGN(2) = 1
1 - 1 = 0
SIGN(0) = 0

Summing the results of these comparisons gives us a number that indicates how many values come before the value that we're examining. It then compares this SUM against COUNT(*) + 1 / 2 to find the middle range...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文