SUM(CASE
WHEN Employee.Salary = alias.Salary THEN 1
ELSE 0
END) >= ABS(SUM(SIGN(Employee.Salary - alias.Salary)))
不难理解,对于这两个元素中的任何一个,其在此数组中大于或小于其自身的数量之差(绝对值)为1,这是元素在不同数组中的确切频率。
因此,通常而言,中位数的频率应等于或大于数组中较大元素和较小元素的绝对差,无论其数量是奇数还是偶数以及它们是否有区别。
从方法1开始{solution from the problem
如何理解报价中的解释?
答案 0 :(得分:0)
您链接中的完整查询是
SELECT
Employee.Id, Employee.Company, Employee.Salary
FROM
Employee,
Employee alias
WHERE
Employee.Company = alias.Company
GROUP BY Employee.Company , Employee.Salary
HAVING SUM(CASE
WHEN Employee.Salary = alias.Salary THEN 1
ELSE 0
END) >= ABS(SUM(SIGN(Employee.Salary - alias.Salary)))
ORDER BY Employee.Id
HAVING子句也可以写为
HAVING SUM(Employee.Salary = alias.Salary)
>=
ABS(SUM(Employee.Salary > alias.Salary) - SUM(Employee.Salary < alias.Salary))
针对不同的薪水值
SUM(Employee.Salary = alias.Salary)
是频率(对于不同的值,它始终为1,因为该行仅与自身匹配)SUM(Employee.Salary > alias.Salary)
是较小值的数量SUM(Employee.Salary < alias.Salary)
是更大值的数量伪代码可能是
HAVING frequency >= abs(smaller_values - bigger_values)
这就是作者所说的:
[..]中位数的频率应等于或大于 大元素和小元素的绝对差异[..]
请注意,如果薪水值并不总是唯一的,则代码的正确“翻译”将是
HAVING frequency * frequency >= abs(smaller_values * frequency - bigger_values * frequency)
但是-结果是相同的。